Attempt to index field ? (A nil Value)

local containerGroup = {} local container for i = 1, 5 do container = display.newRect(i \* 75, 100, 50, 50) container.fill = \_WHITE\_ container:setStrokeColor(0, 0, 1) container.strokeWidth = 3 container = containerGroup[i] end containerGroup[i].x = centerX

I am trying to spawn a set of 5 rectangles to show the player their units and then move it to the center to avoid an issue that occurs on different devices. But when I try to set containers to be part of containerGroup table, I get the Attempt to index newField ?( A Nil Value ) error. Why does this occur?

Also, if anyone can, could they post some in-depth tutorials on tables in Corona? I feel like I still have a lot to learn about them. Thanks!

You just have it the wrong way round. ContainerGroup[i] = container.

You just have it the wrong way round. ContainerGroup[i] = container.

  1. I recognize a color variable from SSK in your code and I can say this is wrong:

    container.fill = _WHITE_

This is correct.

 container:setFillColor( unpack(\_WHITE\_) )
  1. ‘container’ is a terrible name for an object not made with display.newContainer() or display.newGroup().

It implies the object is a container object when it isn’t.

  1. Sorry, but ‘containerGroup’ is a bad name for a table too.  If you use the word ‘Group’ in a name, it strongly implies to future readers of the code that the object is a display group.

Also, this statement is wrong:

containerGroup[i].x = centerX

It is outside the loop, so ‘i’ is no longer defined:

for i = 1, 5 do container = display.newRect(i \* 75, 100, 50, 50) container.fill = \_WHITE\_ container:setStrokeColor(0, 0, 1) container.strokeWidth = 3 container = containerGroup[i] -- MOVE STATEMENT IN LOOP containerGroup[i].x = centerX end

Or, if you’re doing this elsewhere, use a numeric index:

containerGroup[3].x = centerX

Of course, this code won’t work if the table variable is not in scope at the time.

The fully corrected and better named code is as follows:

local objSet = {} -- Don't call this a group, its a table. Again, bad naming choice. for i = 1, 5 do local tmp = display.newRect( i \* 75, 100, 50, 50) objSet[i] = tmp tmp:setFillColor( unpack( \_WHITE\_ ) ) -- Tip: \_W\_ is short name for \_WHITE\_ (See SSK docs) tmp:setStrokeColor( unpack(\_B\_) ) -- \_B\_ is blue in SSK tmp.strokeWidth = 3 end

The later, you can access any object (1…5) as follows:

objSet[3].x = centerX -- 'centerX' Also from SSK

Thanks! Would you happen to know about any tutorials on tables? I’ve looked at the documentation and Corona University videos, but they are not really that in-depth. 

I have a question. I placed the unitSet[i]'s x coordinate to centerX and I realize the mistake I made. How would I have it so that the third rectangle is centered and the other boxes are next to it? Like in this picture, look at where the arrow is pointing.

I am doing this because of issues regarding the positioning of the boxes and the images inside them. 

This is personally my favourite Lua tutorial: http://lua-users.org/wiki/TablesTutorial

Thanks, but I was kind of hoping for something more closely related with Corona. This is Lua separate from Corona, but I will still look into it. Still need help with the above question though. :slight_smile:

With tables though it doesn’t really vary between Corona SDK and barebones Lua, except for the fact that Corona SDK has a few extra table functions that are useful: https://docs.coronalabs.com/api/library/table/index.html. Not sure you’ll find anything more closely related to Corona SDK in terms of a tutorial on tables than what I posted above

Ok, thanks!

Never mind, I got it! (the second question earlier)

 

In case anyone has a similar problem: all I did was set the id for each rectangle to be -3 + i (which is their place in the table, in this case a max of 5), and then I set their x-coordinate to be the center of the screen plus the id multiplied by 75.

 

The end result:

A responsive set of 5 rectangles next to each other that will always appear in the center of the screen.

for i = 1, 5 do unitBox = display.newRect(i \* 75, 100, 50, 50) unitBox.id = -3 + i unitSet[i] = unitBox unitSet[i].x = centerX + unitBox.id \* 75 end

You just have it the wrong way round. ContainerGroup[i] = container.

You just have it the wrong way round. ContainerGroup[i] = container.

  1. I recognize a color variable from SSK in your code and I can say this is wrong:

    container.fill = _WHITE_

This is correct.

 container:setFillColor( unpack(\_WHITE\_) )
  1. ‘container’ is a terrible name for an object not made with display.newContainer() or display.newGroup().

It implies the object is a container object when it isn’t.

  1. Sorry, but ‘containerGroup’ is a bad name for a table too.  If you use the word ‘Group’ in a name, it strongly implies to future readers of the code that the object is a display group.

Also, this statement is wrong:

containerGroup[i].x = centerX

It is outside the loop, so ‘i’ is no longer defined:

for i = 1, 5 do container = display.newRect(i \* 75, 100, 50, 50) container.fill = \_WHITE\_ container:setStrokeColor(0, 0, 1) container.strokeWidth = 3 container = containerGroup[i] -- MOVE STATEMENT IN LOOP containerGroup[i].x = centerX end

Or, if you’re doing this elsewhere, use a numeric index:

containerGroup[3].x = centerX

Of course, this code won’t work if the table variable is not in scope at the time.

The fully corrected and better named code is as follows:

local objSet = {} -- Don't call this a group, its a table. Again, bad naming choice. for i = 1, 5 do local tmp = display.newRect( i \* 75, 100, 50, 50) objSet[i] = tmp tmp:setFillColor( unpack( \_WHITE\_ ) ) -- Tip: \_W\_ is short name for \_WHITE\_ (See SSK docs) tmp:setStrokeColor( unpack(\_B\_) ) -- \_B\_ is blue in SSK tmp.strokeWidth = 3 end

The later, you can access any object (1…5) as follows:

objSet[3].x = centerX -- 'centerX' Also from SSK

Thanks! Would you happen to know about any tutorials on tables? I’ve looked at the documentation and Corona University videos, but they are not really that in-depth. 

I have a question. I placed the unitSet[i]'s x coordinate to centerX and I realize the mistake I made. How would I have it so that the third rectangle is centered and the other boxes are next to it? Like in this picture, look at where the arrow is pointing.

I am doing this because of issues regarding the positioning of the boxes and the images inside them.