Lua - "attempt to compare number with nil" error
Solution 1
The error is on line 11, which means:
if ccnt < yellowEndIndex then
There's your comparison with a number. We know ccnt is a number (it's initialised at the start of the loop), so yellowEndIndex must be nil. 1 < nil is nonsense so that's an error.
Since the error message starts with "Error Occured - " we know it must be coming from your try function error handler. This makes sense. You call:
try(refreshColors, function(e)
print("Error Occured - "..e)
end)
try then invokes:
pcall(f)
where f is refreshColours. This invokes refreshColours without any arguments, i.e. all the arguments are initialised to nil. Of course, invoking refreshColouts with a nil value will naturally attempt to compare 1 (ccnt) to nil (yellowEndIndex)!
You probably want to modify your try function like so:
function try(f, catch_f, ...)
local status, exception = pcall(f, unpack(arg))
if not status then
catch_f(exception)
end
end
So you can call it like:
try(refreshColours, function(e)
print("Error Occured - "..e)
end), 1, 2, 3);
To pass 1, 2 and 3 in as arguments to refreshColours.
Solution 2
Is the error happening because you are calling:
try(refreshColors, function(e) print("Error Occured - "..e) end)
and, the refreshColors has no parameters so that it is indeed nil?
manthosh
Updated on February 12, 2020Comments
-
manthosh about 4 years
a={51,31,4,22,23,45,23,43,54,22,11,34} colors={"white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white"} function try(f, catch_f) local status, exception = pcall(f) if not status then catch_f(exception) end end function refreshColors(yellowEndIndex,redIndex,blueIndex) for ccnt=1,table.getn(a),1 do if ccnt < yellowEndIndex then colors[ccnt] = "yellow" elseif ccnt == redIndex then colors[ccnt] = "red" elseif ccnt == blueIndex then colors[ccnt] = "blue" else colors[ccnt] = "white" end end end try(refreshColors, function(e) print("Error Occured - "..e) end) refreshColors(1,1,1) print(colors[1])
When the refreshColors() function is called, it throws an exception and the error message is "Error Occured - trial.lua:11: attempt to compare number with nil". Why the exception occurs although there is no such comparisons in the refreshColors() function?
-
manthosh about 11 yearsEven after changing the try function as you have specified, the same exception occurs..
-
Martin about 11 yearsThat's very odd, did you try not using try, to make sure the error goes away? (just to make sure my deductions are correct)
-
manthosh about 11 yearsEven now the program runs without any error. I just wanna know why the exception occurs. If i remove the try block its obvious that the exception won't occur.
-
Rich about 11 yearsIt was just 2 minutes newer, but I think that is what I wrote.
-
Martin about 11 yearsI just tried my suggested modification. This program (gist.github.com/martindevans/4758543) seems to do exactly what is expected (if you comment in the appropriate lines at the end).
-
Daniel Kvist about 9 yearsHow can I do this with a function that returns a value?