简体   繁体   English

Lua - “尝试将数字与nil进行比较”错误

[英]Lua - “attempt to compare number with nil” error

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". 当调用refreshColors()函数时,它会抛出异常并且错误消息是“Error Occured - trial.lua:11:尝试将数字与nil进行比较”。 Why the exception occurs although there is no such comparisons in the refreshColors() function? 尽管在refreshColors()函数中没有这样的比较,为什么会发生异常?

The error is on line 11, which means: 错误在第11行,这意味着:

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. 我们知道ccnt是一个数字(它在循环开始时初始化),所以yellowEndIndex必须是nil。 1 < nil is nonsense so that's an error. 1 <nil是胡说八道,所以这是一个错误。

Since the error message starts with "Error Occured - " we know it must be coming from your try function error handler. 由于错误消息以“Error Occured - ”开头,我们知道它必须来自您的try函数错误处理程序。 This makes sense. 这是有道理的。 You call: 你打电话:

try(refreshColors, function(e)
    print("Error Occured - "..e)
end)

try then invokes: 然后尝试调用:

pcall(f)

where f is refreshColours. 其中f是refreshColours。 This invokes refreshColours without any arguments , ie all the arguments are initialised to nil. 这将调用refreshColours 而不带任何参数 ,即所有参数都初始化为nil。 Of course, invoking refreshColouts with a nil value will naturally attempt to compare 1 (ccnt) to nil (yellowEndIndex)! 当然,调用带有nil值的refreshColouts自然会尝试将1(ccnt)与nil(yellowEndIndex)进行比较!

You probably want to modify your try function like so: 你可能想修改你的try函数,如下所示:

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. 将1,2和3作为参数传递给refreshColours。

Is the error happening because you are calling: 是否因为您正在调用错误而发生错误:

try(refreshColors, function(e) print("Error Occured - "..e) end) 尝试(refreshColors,function(e)print(“Error Occured - ”..e)结束)

and, the refreshColors has no parameters so that it is indeed nil? 并且,refreshColors没有参数,所以它确实是零?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试索引零值 - Attempt to index a nil value Lua 设置默认错误处理程序 - Lua set default error handler 在核心数据更改处理期间捕获到异常,尝试用userInfo插入nil(空) - Exception was caught during Core Data change processing, attempt to insert nil with userInfo (null) JNI错误:尝试将TangoPointCloudData的实例传递给onXyzIjAvailable - JNI Error: Attempt to pass an instance of TangoPointCloudData to onXyzIjAvailable 在 std::qsort 的比较函数中处理错误条件 - Handling an error condition in the compare function of std::qsort lua无法解除由hp-ux上的luaL_error生成的异常 - lua can't unwind exception, generated by luaL_error on hp-ux “致命错误:在展开可选值时意外发现 nil”是什么意思? - What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean? 期望Time.zone.parse引发带有无效日期的错误,但返回nil - Expecting Time.zone.parse to raise error with invalid date but return nil 获取异常对象的错误号 - Getting error number of an exception object 我正在尝试插入数据库。n出现错误“ java.lang.IllegalArgumentException:尝试创建具有空实体的saveOrUpdate事件” - I am trying to insert into database.n getting an error “java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM