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

Attempt to perform arithmetic __add on number and nil

Started by Abahu, 25 December 2013 - 01:38 AM
Abahu #1
Posted 25 December 2013 - 02:38 AM
Hello AaP. I've been working on a program for the last three days that monitors the progress of mining turtles via real time mapping. If I desire, I can take over a turtle and control it directly. I've finished most of the program except my function which reads messages and decodes them. My program reads the message one character at a time and stops at each colon, sets this as a variable, and continues until it hits a semi-colon. It worked on one example I ran, and so I copied that example into my main program which is kept in a .txt file. Now here's the problem: The function works until it reaches the end of the line, and then the function returns this error: attempt to perform arithmetic __add on number and nil. What I don't understand is why it's shooting out this error message when I turn my string variables into numbers.

The link to the code is here: http://pastebin.com/SY5ca0mg

The error occurs at line 199. Can anyone help with this? If so, I'd much appreciate it.
tesla1889 #2
Posted 25 December 2013 - 02:46 AM
tonumber is returning nil because it received a string that was not a serialized number

try print(m:sub(1,1)) instead to debug
Abahu #3
Posted 25 December 2013 - 02:55 AM
tonumber is returning nil because it received a string that was not a serialized number

try print(m:sub(1,1)) instead to debug

Yes, I've tried that, but the program only turned up numbers. It didn't turn up any that wasn't an integer. Also, it's only supposed to add when m:sub(l,l) isn't a colon or a semi-colon, as check by my if statements.
oeed #4
Posted 25 December 2013 - 03:24 AM
I don't have the time to look through the entire code, but would changing the preceding if statement to something like the following work?

if m and #m > 0 and m:sub(l,l) ~= ":" and tonumber(m:sub(l,l)) then
Edited on 25 December 2013 - 02:38 AM
Abahu #5
Posted 25 December 2013 - 01:45 PM
I put it in and it throws out an 'expecting a then' error. I don't know what &gt is doing, but I suspect the error is thrown out because of the
#m > 0
section.
oeed #6
Posted 25 December 2013 - 04:01 PM
It should be > not that weird gt thing. It was probably my iPad glitching it.
Edited on 25 December 2013 - 03:01 PM
Abahu #7
Posted 25 December 2013 - 04:32 PM
Haha, okay. That makes more sense.

EDIT: It threw the error "bad argument: number expected, got nil" at line 198.
Edited on 25 December 2013 - 03:37 PM
oeed #8
Posted 25 December 2013 - 05:30 PM
Can post the content of line 198? Also, you could try putting each part of the if (i.e. the parts separated by 'and') on separate lines to tell you which part is failing.
Abahu #9
Posted 25 December 2013 - 05:46 PM
Line 198 is your revised "if" statement. I put them on separate lines (this is in the pastebin of the first post) and it gives me the errors on lines 200 and 201.

while true do
  while true do
   if m then
	if #m > 0 then
	 if m:sub(l,l) ~= ":" then --//Error, expected number got nil
	  if tonumber(m:sub(l,l)) then --//Error, expected number got nil
	   p=p+tonumber(m:sub(l,l))
	   l=l+1
	  else
	   u[i] = p
	   p = 0
	   l = l+1
	   break
	  end
	 end
	end
   end
  end
  i=i+1
  if m:sub(l,l) == ";" then break end
end
end
Edited on 25 December 2013 - 04:47 PM
Abahu #10
Posted 25 December 2013 - 06:20 PM
I fixed the errors, but now it seems to be crashing whenever I use my loading function. Crashing as in it freezes and then the computer turns off. Not sure why.
EDIT: It's fixed. It was crashing because I had nothing to load for my last two maps. Thanks for helping!

What I did: I made sure m was defined and I made p concatenate instead of add.
Edited on 25 December 2013 - 06:03 PM
oeed #11
Posted 25 December 2013 - 08:47 PM
I fixed the errors, but now it seems to be crashing whenever I use my loading function. Crashing as in it freezes and then the computer turns off. Not sure why. EDIT: It's fixed. It was crashing because I had nothing to load for my last two maps. Thanks for helping! What I did: I made sure m was defined and I made p concatenate instead of add.

You're most welcome. An upvote is always appreciated too.