错误:尝试索引字段“?" (零值) [英] Error: attempt to index field '?' (a nil value)

查看:237
本文介绍了错误:尝试索引字段“?" (零值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Lua代码出现以下错误:

I get the following error in my Lua code:

试图索引字段?" (零值)

attempt to index field '?' (a nil value)

它在下面的行中以粗体显示.我该如何解决?

It happens on the line below in bold. How can I fix it?

function SendMessageToAdmins(color1, color2, color3, msg)
    for i = 0, maxSlots - 1 do
        if Account[i] and Account[i].Admin >= 1 or Account[i] and Account[i].GameMaster >= 1 then
            SendPlayerMessage(i, color1, color2, color3, string.format("%s", msg))
        end
    end
end

推荐答案

此错误通常是由于试图为不是表或nil的对象上的字段建立索引而引起的.发生错误时,出现在Account[i]处的不是表或用户数据,而是诸如字符串或数字之类的内置类型.

this error usually comes from trying to index a field on something that isn't a table, or nil. chances are that whatever is at Account[i] when the error happens, isn't a table or userdata, but a built in type like a string or number.

从出现错误时检查Account[i]中任何内容的类型开始,然后从那里开始.

i'd start with checking the type of whatever is in Account[i] when you get that error, and going from there.

以下是我知道的两种最常见的查看此错误的方法:

the two most common ways to see this error (that i know of) are below:

local t = { [1] = {a = 1, b = 2}, [2] = {c = 3, d = 4} }
-- t[5] is nil, so this ends up looking like nil.a which is invalid
-- this doesn't look like your case, since you check for 
-- truthiness in Account[i]
print(t[5].a)

您可能遇到的情况很可能是以下情况:

the case you are probably experiencing, is most likely this one:

local t =
{
    [1] = {a = 1, b = 2},
    [2] = 15, -- oops! this shouldn't be here!
    [3] = {a = 3, b = 4},
}
-- here you expect all the tables in t to be in a consistent format.
-- trying to reference field a on an int doesn't make sense.
print(t[2].a)

这篇关于错误:尝试索引字段“?" (零值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆