attempt to locate index 'sceneGroup' (a nil value)

Hey. So I have been having this problem lately, and can’t get much done with this error constantly showing up…

So it says this:

Game1.lua:188: attempt to locate index ‘sceneGroup’ (a nil value)

stack traceback:

                         Game1.lua:188: in function ‘_listener’

and this is line 188: 

sceneGroup:insert(box4[i])

Which is in this code:

local function spawnDemBowxes4() local sceneGroup = self.view local Fall = math.random( display.contentWidth \* -0.2, display.contentWidth \* 1.2 ) -- this is the new function.. add it.. add a sceneGroup .. try different places box4[i] = display.newImageRect( "box2.png", 38, 38 ) box4[i].x = Fall box4[i].y = -200 sceneGroup:insert(box4[i]) box4[i].collision = onCollision box4[i]:addEventListener( "collision", box4[i] ) transition.to( box4[i], { rotation = box4[i].rotation+720, time=15000, onComplete=spinImage } ) physics.addBody( box4[i], "dynamic" ) box4[i].gravityScale = 0.6 end local function delayTimer() box4Timer = timer.performWithDelay( 500, spawnDemBowxes4, -1 ) end delayTimer = timer.performWithDelay( 25000, delayTimer, 1 ) 

Keep in mind that I have plenty more that are exactly the same as this, but this, is the one that is giving me the error…

the line:

local sceneGroup = self.view

only works inside the Composer event functions. If you’re not using Composer, then you don’t need to add things to the group.  If you’re using Composer and these functions are outside of the scene event functions then you should add:

local sceneGroup = scene.view

at the top of any function where you need to add things to composer.

Rob

So I did change it to

sceneGroup = scene.view

and I even changed all of the “scene:create” “scene:show/hide/destroy” ones too, I’m not sure if sceneGroup = self/scene.view is even supposed to be in those. But even when I took the code I had and the one you gave me out of the “SpawnDemBowxes” function I still get the error.

(This is how you can tell I am a newbie)

scene:create(), scene:show(), scene:hide() and scene:destroy() work with:

local sceneGroup = self.view

Can you post more of your code?

Rob

Well if you want I can post my whole Game1.lua code(301 lines of code…)

That’s the one we are currently discussing.

Although you won’t need to read the entire thing. It just might be better to see how everything is?

I’d prefer not to have the whole module, but at this point I need to see the structure, so its likely you will need to post it all.

Rob

Haha, yeah sorry about that. So I changed everything back to what it was before you gave me the code…

Here goes:(Sorry about not having it numbered, don’t know if there is a setting or anything)

EDIT: I removed the code for now. If you need it, just ask.

Well numbering is a good thing.  If you use the <> button in the formatting bar and you should be able to set a starting line #.  I will need to see the code.

Rob

Code:

EDITED: Code was removed.

There is an important term in programming called “Scope”. Scope is how visible variables and functions are from other parts of the program. Your code has some inconsistent indenting which makes it very hard to see scope.  It took me a while to re-indent the code where I learned a few things

First, you do almost all your work in scene:show() function.  In this instance, you only need to define sceneGroup at the top of scene:show() and remove the local sceneGroup lines from all the functions nested inside scene:show().

You might want to read these two tutorials which will help you with producing easier to manage code:

https://coronalabs.com/blog/2015/06/09/tutorial-the-value-of-well-formatted-code/

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Rob

Alright! Well, thanks!

the line:

local sceneGroup = self.view

only works inside the Composer event functions. If you’re not using Composer, then you don’t need to add things to the group.  If you’re using Composer and these functions are outside of the scene event functions then you should add:

local sceneGroup = scene.view

at the top of any function where you need to add things to composer.

Rob

So I did change it to

sceneGroup = scene.view

and I even changed all of the “scene:create” “scene:show/hide/destroy” ones too, I’m not sure if sceneGroup = self/scene.view is even supposed to be in those. But even when I took the code I had and the one you gave me out of the “SpawnDemBowxes” function I still get the error.

(This is how you can tell I am a newbie)

scene:create(), scene:show(), scene:hide() and scene:destroy() work with:

local sceneGroup = self.view

Can you post more of your code?

Rob

Well if you want I can post my whole Game1.lua code(301 lines of code…)

That’s the one we are currently discussing.

Although you won’t need to read the entire thing. It just might be better to see how everything is?

I’d prefer not to have the whole module, but at this point I need to see the structure, so its likely you will need to post it all.

Rob

Haha, yeah sorry about that. So I changed everything back to what it was before you gave me the code…

Here goes:(Sorry about not having it numbered, don’t know if there is a setting or anything)

EDIT: I removed the code for now. If you need it, just ask.

Well numbering is a good thing.  If you use the <> button in the formatting bar and you should be able to set a starting line #.  I will need to see the code.

Rob

Code:

EDITED: Code was removed.

There is an important term in programming called “Scope”. Scope is how visible variables and functions are from other parts of the program. Your code has some inconsistent indenting which makes it very hard to see scope.  It took me a while to re-indent the code where I learned a few things

First, you do almost all your work in scene:show() function.  In this instance, you only need to define sceneGroup at the top of scene:show() and remove the local sceneGroup lines from all the functions nested inside scene:show().

You might want to read these two tutorials which will help you with producing easier to manage code:

https://coronalabs.com/blog/2015/06/09/tutorial-the-value-of-well-formatted-code/

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Rob