Jump to content




FS API h.readAll() returns 'attempt to index ? (a nil value)'


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

#1 Rsstn

  • Members
  • 53 posts
  • LocationLondon, UK

Posted 21 February 2013 - 03:44 AM

Hi guys, haven't been here in a while!
I am starting my own email programs for a server with friends and have started by giving them an easy way to change what side the modem is attached to (the don't know Lua).
I started with this: http://pastebin.com/4gsvLThm (The pastebin link is better because the colour is correct)

Spoiler

The error I get is:

Quote

FILENAME:19: attempt to index ? (a nil value)

I've seen this error many a time before, but I just can't work out what's going wrong. It seems that 'h.readAll()' is returning nil, and I can't work out why. I've done this before and not had trouble, and tried using 'h.readLine()' too.
This is all correct according to the wiki, but maybe it's just that I'm out of practice in Lua.
(and yes I know it could be neater, but I just want to get it working for now!)

Thanks in advance,
Ross.

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 21 February 2013 - 04:19 AM

Check that the file you are trying to open exists.

#3 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 21 February 2013 - 04:20 AM

That means h is nil. Make sure that the file you're trying to open does actually exist.

EDIT Ninja'd... nevermind...

#4 Rsstn

  • Members
  • 53 posts
  • LocationLondon, UK

Posted 21 February 2013 - 04:35 AM

Ah, ok that might be it. I forgot the code to write the file comes after the code to read it!
I'll assume that's the problem and give it a go later. Thanks for your help guys!

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 February 2013 - 04:38 AM

to prevent it in the future do this

h = fs.open("EmailData/ModemData", "r")
if not h then error("Could not open the file for read") end
modemSide = h.readAll()
h.close()


that way when you get an error in the future it makes a little more sense.

#6 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 21 February 2013 - 04:45 AM

View PostTheOriginalBIT, on 21 February 2013 - 04:38 AM, said:

to prevent it in the future do this

h = fs.open("EmailData/ModemData", "r")
if not h then error("Could not open the file for read") end
modemSide = h.readAll()
h.close()


that way when you get an error in the future it makes a little more sense.

h = assert(fs.open('EmailData/ModemData', 'r'), "Could not open the file for read")


#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 February 2013 - 05:05 AM

View PostremiX, on 21 February 2013 - 04:45 AM, said:

h = assert(fs.open('EmailData/ModemData', 'r'), "Could not open the file for read")
Also works yes. would think i would have used that given the error handling tutorial I did, and recently updating it with more on assert. wonder why i didn't.

#8 Rsstn

  • Members
  • 53 posts
  • LocationLondon, UK

Posted 21 February 2013 - 05:09 AM

Ok, here is my new code: http://pastebin.com/wx6ssA7X
But whenever I run the code, the computer goes blank (shutting down? crashing?)
When I right-click on the PC again, it boots up like normal with 'CraftOS 1.5'.
What's going on here?

#9 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 21 February 2013 - 05:13 AM

its most likely too long without yeilding
because your yeild is encased in
if modemSide == "left" or modemSide == "right" or modemSide == "top" or modemSide == "bottom" or modemSide == "back" or modemSide == "front" then


#10 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 21 February 2013 - 05:21 AM

View PostPixelToast, on 21 February 2013 - 05:13 AM, said:

its most likely too long without yeilding

In other words (sorry Pixel, I don't think that's very clear) you have to swap the "if modemSide" and the "while true do". Cuz if the IF returns false, it just keeps checking modemSide in an infinite loop.

#11 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 21 February 2013 - 05:26 AM

that means there is another problem, modemSide isnt any of those sides

#12 Rsstn

  • Members
  • 53 posts
  • LocationLondon, UK

Posted 21 February 2013 - 05:39 AM

Ok, I found that it was stopping because my program did not yeild, and the screen stayed blank because there was no 'else' part of the if statement. (wasn't supposed to be needed!)
Now I got to find out why the ASCII codes arn't correct...

EDIT: Ah, you got there first!

#13 Rsstn

  • Members
  • 53 posts
  • LocationLondon, UK

Posted 21 February 2013 - 05:41 AM

But the 'write()' line near the top should have sorted that out and set it to the default (right) side...

#14 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 21 February 2013 - 05:44 AM

idk, aslong as it dosent error now you sould be fine





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users