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

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

Started by theeboris, 03 March 2013 - 05:23 AM
theeboris #1
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?
Bubba #2
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).
theeboris #3
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'
bjornir90 #4
Posted 04 March 2013 - 04:33 AM
I think it's because a is nil
remiX #5
Posted 04 March 2013 - 05:25 AM
Remove the write(version.readAll()) and then it will work
theeboris #6
Posted 04 March 2013 - 05:30 AM
Thanks remix! It works.
Bubba #7
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.
theeboris #8
Posted 04 March 2013 - 05:48 AM
Thanks bubba. Now it understan it.
remiX #9
Posted 04 March 2013 - 06:01 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.

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.
Bubba #10
Posted 04 March 2013 - 06:27 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.

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.
remiX #11
Posted 04 March 2013 - 06:29 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.

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
Bubba #12
Posted 04 March 2013 - 06:31 AM
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
oeed #13
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.