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

A Bug With My Code...

Started by MayContainVennom, 28 July 2013 - 10:10 AM
MayContainVennom #1
Posted 28 July 2013 - 12:10 PM
Hello there,

I've been developing a code to print information from a file to a screen. I originally was using functions causing it to error out after about half an hour, I have just changed the code to not have functions in it and now after about 30 mins of run time the programs errors out saying 'startup:12: attempt to index ? (a nil value)' or something similar. But this dose not happen instantly like I said before. I don't know what to do. (Note the file is saved on a disk so that may be moved out of the drive to 'reset' it. That is why I have the first if statement.

Code:
Spoilerm = peripheral.wrap("right")

while true do
if fs.exists("disk/Tasks") then
m.clear()
term.setCursorPos(1,1)
term.redirect(m)
f = fs.open("disk/Tasks", "r")
print(f.readAll())
sleep(5)
else
print("Disk not found checking again in 5 Seconds.")
sleep(5)
end
end
GopherAtl #2
Posted 28 July 2013 - 12:22 PM
after the "print( f.readAll()" you need to do "f.close()". By not doing so, you're leaving references to the file open every time it runs, and eventually exhausting the supply of file references available to you.
KaoS #3
Posted 28 July 2013 - 12:23 PM

term.redirect(peripheral.wrap("right"))
while true do
  if fs.exists("disk/Tasks") then
	term.clear()
	term.setCursorPos(1,1)
	f = fs.open("disk/Tasks", "r")
	print(f.readAll())
	f.close()
	sleep(5)
  else
	print("Disk not found checking again in 5 Seconds.")
	sleep(5)
  end
end

You should always close your file handles. Also you only have to term.redirect once

EDIT
Edited on 28 July 2013 - 10:24 AM
GopherAtl #4
Posted 28 July 2013 - 12:24 PM
ah, good catch, I didn't even notice the term.redirect, was just looking for what would cause the null error.
MayContainVennom #5
Posted 28 July 2013 - 12:26 PM
Wow can't believe I forgot that I had it in the first version >.< Thanks anyway!