This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
bbaayyeerr's profile picture

attempt to index ? (a nil value)

Started by bbaayyeerr, 21 June 2015 - 10:02 PM
bbaayyeerr #1
Posted 22 June 2015 - 12:02 AM
Hello guys!
I would like to start of with saying that I've reed the "Read this post before asking questions", since my error message is covered quite early in that post, but i still can not find a solution. Everything seams fine in my eyes, but then again I've been staring at this error trying to figure out a solution, for the last hour or so… I would be happy if there were someone that could explain what I'm doing wrong! :)/>/>

The error message mark the line "file.write(textutils.seri…)" as the source of failure. ("attempt to index ? (a nil value)")


dir = "client/main"

function saveUserInfo(dir)
	local username = {"user1", "user2", "user3"}  -- Here you need to define all defaul usernames
	local password = {"password1", "password2", "password3"}  -- Here you define what password each default user has

	local file = fs.open("UserInfo/"..dir, "w")
	print("Successfully opened file...") --debugging
	file.write(textutils.serialize(username).."\n"..textutils.serialize(password))
	file.close()

Thanks in advance!
//bbaayyeerr
Edited on 21 June 2015 - 11:16 PM
Quintuple Agent #2
Posted 22 June 2015 - 12:13 AM
I just tried this on a computer and it worked fine.
Check and make sure you copied everything over verbatim. You might have an error in your code but you fixed it when typing the text again into the forum.
bbaayyeerr #3
Posted 22 June 2015 - 12:20 AM
I just tried this on a computer and it worked fine.
Check and make sure you copied everything over verbatim. You might have an error in your code but you fixed it when typing the text again into the forum.

How strange! The only difference between the code that i have and the one above is that dir corresponds to dir = "client/"..data[4] where data[4] is a table with a string ("main") in the fourth position… I believe that i might have to take another look at the part that deals with what directory gets inserted into the function.
TheOddByte #4
Posted 22 June 2015 - 12:23 AM
My guess is that it didn't return a file handle, to make sure it did, try this

local file = fs.open( path, mode )
if not file then
    error( "Could not open file for writing", 2 )
end
If it errored then you've probably put in an incorrect path, like for example trying to write to a file in a folder that doesn't exist( I believe that will throw an error )
Quintuple Agent #5
Posted 22 June 2015 - 12:29 AM
Weird, I just ran

data={"this","is","a","test"}
dir = "client/"..data[4]
function saveUserInfo(dir)
		local username = {"user1", "user2", "user3"}  -- Here you need to define all default usernames
		local password = {"password1", "password2", "password3"}  -- Here you define what password each default user has

		local file = fs.open("UserInfo/"..dir, "w")
		print("Successfully opened file...") --debugging
		file.write(textutils.serialize(username).."\n"..textutils.serialize(password))
		file.close()
end
saveUserInfo(dir)
and it worked, created the correct directories UserInfo/client with the file inside and appeared as

{
   "user1",
   "user2",
   "user3",
}
{
   "password1",
   "password2",
   "password3",
}

My guess is that it didn't return a file handle, to make sure it did, try this
Yah I started to wonder if he has something confliting like he has a file named UserInfo or UserInfo/client or a directory inside UserInfo/client/ named main

He could also do print(type(file)) just to see what it is returning.
Edited on 21 June 2015 - 10:31 PM
bbaayyeerr #6
Posted 22 June 2015 - 12:36 AM
My guess is that it didn't return a file handle, to make sure it did, try this

local file = fs.open( path, mode )
if not file then
	error( "Could not open file for writing", 2 )
end
If it errored then you've probably put in an incorrect path, like for example trying to write to a file in a folder that doesn't exist( I believe that will throw an error )

Got the error! :S Then i probably need to create the directory "client" before trying to write a file to it! ;)/>

I guess that i don't have to create a directory after all then…
Edited on 21 June 2015 - 10:53 PM
Quintuple Agent #7
Posted 22 June 2015 - 12:38 AM
Got the error! :S Then i probably need to create the directory "client" before trying to write a file to it! ;)/>

Possibly. If that fixes it, that is great.
It is strange though, since doing fs.open("blah/halb/wubwub/test.jpg","w") should create all the of needed directories. It did when I ran your code, it created both UserInfo and UserInfo/client
Just wondering, what version of CC are you using?
Edited on 21 June 2015 - 10:39 PM
TheOddByte #8
Posted 22 June 2015 - 12:46 AM
- Snip -
If he's using an older version of CC then that may be the problem, that it doesn't automatically create all the directories for you.
bbaayyeerr #9
Posted 22 June 2015 - 12:50 AM
Oooh, Computercraft 1.58 didn't think of that! :unsure:/>/>
Sorry for the inconvenience, I should have mentioned the CC version earlier… Running FTB Monster, rather low version of the mod I would say, should probably update it now when I've realized how far bak I am in versions! xD
Edited on 21 June 2015 - 11:21 PM
Quintuple Agent #10
Posted 22 June 2015 - 01:37 AM
Well if you need it, you could use this function to print out your data table

function checkData()
	for i,d in pairs(data) do
	 print("Indx:"..i..":"..type(d).." "..tostring(d))
end
end
and if for some reason the directories are not being created by fs.open, you can use this

function mkDirTree(tempDir)
    local full=""
    for cd in string.gmatch(tempDir,"%w+/") do
        full=full..cd
        if not fs.exists(full) then
            fs.makeDir(full)
            print("Made "..full)
        else
            print(full.." Exists")
        end
    end
return
end
Just make sure when you call it you do

mkDirTree("UserInfo/"..dir)
instead of

mkDirTree(dir)
(I forgot about that for like 3 minutes and was wondering why it was just creating client and not the UserInfo <_</> )
Edited on 21 June 2015 - 11:40 PM