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

[Solved][Lua][Error] Attempt to index ? (a nil value).

Started by AadamZ5, 08 May 2013 - 07:02 PM
AadamZ5 #1
Posted 08 May 2013 - 09:02 PM
Well, I have some sort of nil error. At first, a user enters a command. The command takes it to it's elseif. I plan on making it functions later. Anyways, when a user enters find, it prompts them on what they want to find. So if a user enters something that is actualy a file, it reads it. But, if a user enters a non valid file, i get Error. "attempt to index ? (a nil value)" Obviosly, nothing is there. but the code should catch that with:


local locate = read()
		exist = fs.exists(locate)
		if exist == "false" then
			print("Error. Item not found.")
			sleep(2)
		else
			(ect)

But it doesn't detect that the file is not there.
So it tries to read the file that isnt there, and that is where I BELIEVE my error is.
Somebody help me.

Code:

function clear()
	term.clear()
	term.setCursorPos(1, 1)
end

while true do
	clear()
	print("What would you like to do?\nValid Commands are: \nadd \nfind \ndelete\n")
	local input = read()
	if input == "add" then
		clear()
		term.write("What is the name of the item?\n")
		local itemn = read()
		local exist = fs.exists(itemn)
		if exist == "true" then
			clear()
			print("Error. That already exists.")
			sleep(2)
		else
			clear()
			print("How much does it cost?")
			local itemc = read()
			fs.makeDir("ItemBase")
			h = fs.open(itemn, "w")
			h.write("item: "..itemn.." Cost: "..itemc)
			h.close()
			clear()
			print("Item Added.")
			sleep(2)
			clear()
		end
	elseif input == "find" then
		clear()
		print("What would you like to find?")
		local locate = read()
		exist = fs.exists(locate)
		if exist == "false" then
			print("Error. Item not found.")
			sleep(2)
		else
			clear()
			h = fs.open(locate, "r")
			local data = h.readLine()
			h.close()
			print(data)
			sleep(5)
		end
	elseif input == "delete" then
		print("What do you want to delete?")
		local del = read()
		delexist = fs.exists("ItemBase/"..del)
		if delexist == "false" then
			clear()
			print("Error. Item not found.")
			sleep(2)
		else
			fs.delete(del)
			clear()
			print("Item deleted.")
			sleep(2)
		end
	end
end
LBPHacker #2
Posted 09 May 2013 - 12:49 PM
You know, true and false are "boolean" values, not string values. If you want to look for the logic false, just use it without quotation marks:
if exist == false then

BUT: When an expression inside the IF returns only true or false, you don't even have to check if it's false or not - what I mean:
local isThisTrue = (1 == 1) -- obviously true
-- isThisTrue is a "boolean" true now, so you can give it to the IF as it is:
if isThisTrue then

And of course, if you want to catch a boolean false without using ==, you can use the NOT keyword:
if not isThisTrue then
AadamZ5 #3
Posted 09 May 2013 - 05:58 PM
Oh, Thanks, Alot.
Didn't know that Booleans weren't strings.

Worked. Thanks.