Jump to content




A Bug With My Code...


  • You cannot reply to this topic
4 replies to this topic

#1 MayContainVennom

  • Members
  • 47 posts

Posted 28 July 2013 - 11:10 AM

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:
Spoiler


#2 GopherAtl

  • Members
  • 888 posts

Posted 28 July 2013 - 11:22 AM

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.

#3 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 28 July 2013 - 11:23 AM

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 by KaoS, 28 July 2013 - 11:24 AM.


#4 GopherAtl

  • Members
  • 888 posts

Posted 28 July 2013 - 11:24 AM

ah, good catch, I didn't even notice the term.redirect, was just looking for what would cause the null error.

#5 MayContainVennom

  • Members
  • 47 posts

Posted 28 July 2013 - 11:26 AM

Wow can't believe I forgot that I had it in the first version >.< Thanks anyway!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users