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

Ask A Pro [I need help debugging]

Started by JustIan12, 03 June 2016 - 06:28 PM
JustIan12 #1
Posted 03 June 2016 - 08:28 PM
I have spent the past few hours googling the issue and Nothing has come up I am trying to learn the more advanced stuff in CC and well I would like to know what is causing this error and what the error message means so this is a Key Card program that I wrote and the first half works but when I put in a incorrect key card the program crashes. any ideas ??

Code: http://pastebin.com/wYrucN1t

Error: key:26: attempt to index ? (a nil value)
Emma #2
Posted 03 June 2016 - 10:21 PM
Attempt to index ? (a nil value)
Means that you are trying to retrieve an element from a nonexistent (aka nil) table.
When you call fs.open(foo, "r"), if foo does not exist, then the function returns nil. So, when you try to call h.readAll() it fails because h is nil.
To solve this, add a nil check before you try to read.
Ex

if h ~= nil and h.readAll() == data then
    --Do your code here
end
The h~=nil will evaluate to true if h is not nil, and false if it is. And when the statement before an 'and' token is false, it quits, and evaluates the whole expression to false without attempting to call h.readAll()
KingofGamesYami #3
Posted 03 June 2016 - 11:03 PM
Actually, the script has a more serious problem: It never closes h. Because of this, the file opened is unavailable to any program (or program instance) until the computer is rebooted.

It is also customary to check for the existance of a file before attempting to open it using fs.exists.


function mainLoop()

 while true do
  os.pullEvent("disk", driveSide)
  h = fs.open("disk/database", "r")

  if h.readAll() == data then
   h.close() --#ALWAYS CLOSE YOUR FILE HANDLES
   rs.setOutput(rsSide, true)
   disk.eject(driveSide)
   sleep(2)
   rs.setOutput(rsSide, false)

  else
   h.close() --#ALWAYS CLOSE YOUR FILE HANDLES
   sleep(1)
   disk.eject(driveSide)
  end
 end
end
JustIan12 #4
Posted 06 June 2016 - 08:00 PM
Updated the code, closing the file handles like you said KingOfGamesYami and I also added a nil check like you said Incinicrate but the code returns the error.

startup:34: attempt to index ? (a nil value)

function mainLoop()

while true do
  os.pullEvent("disk", driveSide)
  h = fs.open("disk/database", "r")

  if h ~= nil and h.readAll() == data then
   h.close()
   rs.setOutput(rsSide, true)
   disk.eject(driveSide)
   sleep(2)
   rs.setOutput(rsSide, false)

  else
   h.close()
   sleep(1)
   disk.eject(driveSide)
  end
end
end
Emma #5
Posted 06 June 2016 - 10:10 PM
Updated the code, closing the file handles like you said KingOfGamesYami and I also added a nil check like you said Incinicrate but the code returns the error.

startup:34: attempt to index ? (a nil value)

function mainLoop()

while true do
  os.pullEvent("disk", driveSide)
  h = fs.open("disk/database", "r")

  if h ~= nil and h.readAll() == data then
   h.close()
   rs.setOutput(rsSide, true)
   disk.eject(driveSide)
   sleep(2)
   rs.setOutput(rsSide, false)

  else
   h.close()
   sleep(1)
   disk.eject(driveSide)
  end
end
end

The nil problem that applies to h.readAll() also applies to h.close()
Change line 36 to this:

if h ~= nil then 
    h.close()
end
Same reasons apply that I made note of above in my first post.
JustIan12 #6
Posted 06 June 2016 - 11:46 PM
IT WORKED!!!! it doesent crash anymore the only issue now is that it does not eject the floppy the incorrect one is inserted.
Edited on 07 June 2016 - 02:21 PM