Scicare
Scicare

Reputation: 833

attempt to index field ? (nil value)

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

Upvotes: 2

Views: 12907

Answers (1)

kikito
kikito

Reputation: 52641

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.

Upvotes: 7

Related Questions