Array/table problem gives nil value

Hello. While working with arrays/tables I came across a problem

 for x = 0, kLevelCols do  
 levelVisual[x] = {}  
 for y = 0, kLevelRows do   
 if level[x][y] == 2 then  
 --Problem comes here  
 if level[x-1][y] == 2 then   
 levelVisual[x][y] = 1  
 end  
 elseif level[x][y] == 1 then  
 levelVisual[x][y] = 1  
 else  
 levelVisual[x][y] = 0  
 end  
 end  
 end  

I got 2 tables named level and levelVisual. level contains values of either 0, 1 or 2. Within the for-loop, I can access the x, y elements of level normally. However, if I try to access either x+1 or y+1, Corona tells me I try to access a nil value, even if it isn’t.

Is there anything lua sensitive that I overlooked? [import]uid: 191248 topic_id: 34991 reply_id: 334991[/import]

I’m not seeing anything obvious, but for this code to work, level[x][y] must always exist. If level[x][y] doesn’t exist, then regardless of the fact that level[x][y] *technically* ~= 2, it will just give you an error. You may want to see how the code does if you add a table check first, like

if level[x][y] and level[x][y] == 2 then -- The second part of the "and" is not looked at unless the first part is true

It’s hard to tell beyond that what could be specifically wrong except to say that not knowing the source tables, it’s possible for this code to try evaluating level[-1][0] which I don’t think is possible. (0 is possible, -1 I don’t think is…) [import]uid: 41884 topic_id: 34991 reply_id: 139208[/import]

Hmm your for loops are starting at 0. Arrays start at 1. I am not sure if your actual code shares the same problem. [import]uid: 56820 topic_id: 34991 reply_id: 139229[/import]

That’s a good point. I just assumed he manually created the tables with a 0 entry (zero is allowed as an array index, but is never automatically made) [import]uid: 41884 topic_id: 34991 reply_id: 139247[/import]

Ah! That solved the problem XD Silly me.
Thanks :smiley: [import]uid: 191248 topic_id: 34991 reply_id: 139304[/import]

I’m not seeing anything obvious, but for this code to work, level[x][y] must always exist. If level[x][y] doesn’t exist, then regardless of the fact that level[x][y] *technically* ~= 2, it will just give you an error. You may want to see how the code does if you add a table check first, like

if level[x][y] and level[x][y] == 2 then -- The second part of the "and" is not looked at unless the first part is true

It’s hard to tell beyond that what could be specifically wrong except to say that not knowing the source tables, it’s possible for this code to try evaluating level[-1][0] which I don’t think is possible. (0 is possible, -1 I don’t think is…) [import]uid: 41884 topic_id: 34991 reply_id: 139208[/import]

Hmm your for loops are starting at 0. Arrays start at 1. I am not sure if your actual code shares the same problem. [import]uid: 56820 topic_id: 34991 reply_id: 139229[/import]

That’s a good point. I just assumed he manually created the tables with a 0 entry (zero is allowed as an array index, but is never automatically made) [import]uid: 41884 topic_id: 34991 reply_id: 139247[/import]

Ah! That solved the problem XD Silly me.
Thanks :smiley: [import]uid: 191248 topic_id: 34991 reply_id: 139304[/import]