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

[Lua][Error] :61: Attempt to index ? (a nil value) [SOLVED]

Started by Espiroth, 29 May 2012 - 09:58 PM
Espiroth #1
Posted 29 May 2012 - 11:58 PM
I'm doing something wrong, and I don't know what it is, can someone have a look and point my booboo out…

This programs goal is to write multiple files to a floppy so as to record name, clearance, etc so that when the punch in to the system, it can be logged.

I'm not sure if it has something to do with line 61


print ("Corsair Security Card Programming Suite")

HorzLoadBar(5, 45, "/")

print ("Insert blank card")
while true do
os.pullEvent()
if disk.isPresent(dside) then
print ("Card detected")
write ("Enter clearance level of card: >")
local clearance = read()
write ("Enter purpose of card: >")
local purpose = read()
write ("Enter ID# of card: >")
local id = read()
write ("Enter name of user: >")
local name = read()
disk.setLabel(dside, "Level "..clearance.." Access card")
file = io.open("disk/data/clearance", "w")
file:write(clearance) -------------------------------------------->LINE 61
file:close()
file = io.open("disk/data/purpose", "w")
file:write(purpose)
file:close()
file = io.open("disk/data/name", "w")
file:write(name)
file:close()
print ("Card has successfuly been programmed.")
sleep(2)
print ("Please remove the card")
sleep(2)
print ("Rebooting...")
sleep(1)
os.reboot()
end
end
MysticT #2
Posted 30 May 2012 - 12:06 AM
Always check the handle returned when opening files:

local file = fs.open("path", "mode") -- also with io.open
if file then
  -- do something with the file
  file.close() -- file:close() with io.open()
end
The problem on your code is that the file can't be created because the folder where you try to open it doesn't exits (the folder "data" inside "disk").
Create the directory first:

fs.makeDir("/disk/data")
before creating the file (in file = io.open("disk/data/clearance", "w")).
Espiroth #3
Posted 30 May 2012 - 12:25 AM
Always check the handle returned when opening files:

local file = fs.open("path", "mode") -- also with io.open
if file then
  -- do something with the file
  file.close() -- file:close() with io.open()
end
The problem on your code is that the file can't be created because the folder where you try to open it doesn't exits (the folder "data" inside "disk").
Create the directory first:

fs.makeDir("/disk/data")
before creating the file (in file = io.open("disk/data/clearance", "w")).


Thanks! I didn't even think of the directory, now everything works great!