attempt to index field ? (nil value)

你。 提交于 2020-05-15 08:42:13

问题


I am not sure where the problem is. Anyone know why?

function check(board, color, row, col)
--if same color, change tile to "o"

if board[row][col] == color then -- attempt to index nil?
    board[row][col] = "o"
    count = count + 1
    return "o"
end

return

end


回答1:


The problem is that board[row] is not defined; it's nil. So you are trying to do nil[col].

You can avoid this error by doing this:

if board[row] and board[row][col] == color then

Instead.

However, I'd recommend you to review the way board is created - for example, make sure that you have not switched rows and cols somewhere in your code by mistake.



来源:https://stackoverflow.com/questions/9397117/attempt-to-index-field-nil-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!