Jump to content




[Error][Fixed]startup:20: attempt to compare nil with number


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

#1 theeboris

  • Members
  • 59 posts
  • LocationThe Nethelands

Posted 03 March 2013 - 06:23 AM

Hello I have a problem. If I use
if http.request("http://lightos.webuda.com/lightos/version.inf") >= version then
version = false
end
I got the error 'startup:20: attempt to compare nil with number'. How can I fix this?

#2 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 03 March 2013 - 06:33 AM

http.request does not return a value. You'll need to use events to capture the http_success or http_failure, as well as the web function table.
http.request("http://www.google.com/")
local e = {os.pullEvent()}
if e[1] == "http_failure" then
   print("Uh oh! Couldn't connect to google!")
   return
end
local content = e[3].readAll()
e[3].close()
print(content)

I also noticed that you are trying to compare the result of an http request (a string) directly to a number. You need to convert the string into a number with tonumber(content).

#3 theeboris

  • Members
  • 59 posts
  • LocationThe Nethelands

Posted 04 March 2013 - 03:48 AM

I have
local version = http.get("http://lightos.webuda.com/lightos/version.inf")
write(version.readAll())
a = version.readAll()
version.close()
if tonumber(a) >= 011 then
uptodate = false
en
now I got 'startup:23: attempt to compare nil with number'

#4 bjornir90

  • Members
  • 378 posts
  • LocationFrance

Posted 04 March 2013 - 04:33 AM

I think it's because a is nil

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 04 March 2013 - 05:25 AM

Remove the write(version.readAll()) and then it will work

#6 theeboris

  • Members
  • 59 posts
  • LocationThe Nethelands

Posted 04 March 2013 - 05:30 AM

Thanks remix! It works.

#7 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 04 March 2013 - 05:33 AM

Just in case you were wondering why it didn't work, you used http.get as opposed to http.request which is the one I used. They return different values.

#8 theeboris

  • Members
  • 59 posts
  • LocationThe Nethelands

Posted 04 March 2013 - 05:48 AM

Thanks bubba. Now it understan it.

#9 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 04 March 2013 - 06:01 AM

View PostBubba, on 04 March 2013 - 05:33 AM, said:

Just in case you were wondering why it didn't work, you used http.get as opposed to http.request which is the one I used. They return different values.

No, the problem is that doing readAll() causes it go be at the very last line (which is nil if read) and then he does another readAll() which returns nil.
It's like doing readLine() and then readLine() again - the second readLine() will read the second line and not the first.

#10 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 04 March 2013 - 06:27 AM

View PostremiX, on 04 March 2013 - 06:01 AM, said:

View PostBubba, on 04 March 2013 - 05:33 AM, said:

Just in case you were wondering why it didn't work, you used http.get as opposed to http.request which is the one I used. They return different values.

No, the problem is that doing readAll() causes it go be at the very last line (which is nil if read) and then he does another readAll() which returns nil.
It's like doing readLine() and then readLine() again - the second readLine() will read the second line and not the first.

Ah yes. My mistake. Please ignore my response: foolish http noob here.

#11 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 04 March 2013 - 06:29 AM

View PostBubba, on 04 March 2013 - 06:27 AM, said:

View PostremiX, on 04 March 2013 - 06:01 AM, said:

View PostBubba, on 04 March 2013 - 05:33 AM, said:

Just in case you were wondering why it didn't work, you used http.get as opposed to http.request which is the one I used. They return different values.

No, the problem is that doing readAll() causes it go be at the very last line (which is nil if read) and then he does another readAll() which returns nil.
It's like doing readLine() and then readLine() again - the second readLine() will read the second line and not the first.

Ah yes. My mistake. Please ignore my response: foolish http noob here.

Not only http :D Also with fs/io api for opening files for reading

#12 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 04 March 2013 - 06:31 AM

View PostremiX, on 04 March 2013 - 06:29 AM, said:

Not only http :D Also with fs/io api for opening files for reading

Nah I'm perfectly fine with the fs api. I just missed the fact that he used f.readAll() twice (didn't read through it very carefully).
Thank you for being so quick to confirm my idiocy though XD

#13 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 04 March 2013 - 09:54 AM

You may want to make your title a bit more precise in future... '[Error]' isn't exactly descriptive.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users