attempt to index field 'contentBounds' (a nil value)

I am creating an app with start.lua , game.lua and restart.lua scenes . When I click start I go into the game , play the game smoothly . When I die I go to the restart.lua scene . I click restart and go back to game.lua . I can do this a number of times until a certain point where I get this error :

ERROR: Runtime error ?:0: attempt to index field 'contentBounds' (a nil value) stack traceback: ?: in function '\_saveSceneAndHide' ?: in function 'gotoScene' restart.lua:26: in function '?'

I did not add a _saveSceneAndHide function to any of my scenes , Can someone help me with this problem ?

restart.lua:

-- requires local composer = require( "composer" ) local scene = composer.newScene() -- background function scene:create(event) local screenGroup = self.view local randomImage = math.random(1,33) local background = display.newImageRect("images/background"..randomImage..".jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) local restart = display.newText( "Touch Screen to Play Again", display.contentCenterX, display.contentCenterY, native.systemFont, 25 ) screenGroup:insert(restart) end local function touchScreen( event ) if event.phase == "began" then composer.gotoScene("game", "fade", 400) --line 26 where is error is happening end end function scene:show(event) composer.removeScene( "game" ) Runtime:addEventListener("touch", touchScreen) end function scene:hide(event) display.remove(currentTimeText) Runtime:removeEventListener("touch", touchScreen) end function scene:destroy(event) end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene

That’s not how you call composer.gotoScene():

https://docs.coronalabs.com/api/library/composer/gotoScene.html

Do I have to put all of this ?

-- Later... local options = { effect = "fade", time = 800, params = { level="Level 1", score=currentScore } } composer.gotoScene( "results", options )

This is what I have and I’m getting the same error:

 local options = { effect = "fade", time = 400, } local function touchScreen( event ) if event.phase == "began" then composer.gotoScene( "game", options ) end end

I know this doesn’t answer your question but I’d also highly suggest seeding your random call. Take a look at math.randomseed() which will give you more random numbers. Also make sure that the last item in your array doesn’t have a comma at the end (t = {1, 2, 3,}; is not correct while t = {1, 2, 3}; is). Not sure that would cause that error but it definitely isn’t correct afaik

The problem is in game.lua

Clearly, somewhere in that file you have a statement including this text ’ .contentBounds’,  That line is the problem.

- OR -

You have a bug in the code that is being found as soon as the file (game.lua) is loaded.  (See my comment from 30 MAY further down in this thread.)

Find that line in game.lua and you’ll be well on your way to resolving this.

10$ says you’ve got a typo, or a scope issue and are trying to get the bounds of a nil variable.

Question: Did you grep your game for the word ‘contentBounds’?  In the future do this and you should be better equipped to find the issue.

If you’re an OS X user, invest in a grep tool or use the command line.

If you’re on Windows I highly suggest ‘Agent Ransack’.

https://forums.coronalabs.com/topic/49722-file-attempt-to-index-field-contentbounds-a-nil-value/

The error points to a situation in composer.gotoScene() where we are trying to save and hide the current scene. We attempt to get the contentBounds of the currentScene. We are not testing for the scene to be nil before trying to fetch it’s bounding box. We probably should harden that.

Some how in your code you’ve managed to get the current scene to be nil. It’s like you’ve managed the remove the scene you are currently in. I would look at your code and make sure you’re not trying to remove the current scene.

Rob

I’ve seen this happen when the incoming scene has a syntax error that is found when the scene file is first loaded.

The easiest way I’ve found to ‘dig into’ these errors is to use a trick.

Require the scene file and see what errors pop out.

In main.lua do this:

require "game"

If it produces errors, fix the errors if not, remove this line and move on to the next step in debugging.

I did this 

local composer = require( "composer" ) composer.gotoScene("start") require "game"

and no errors showed

That’s not what I suggested.

Just put the require “game” at the top of main.lua before any calls to composer.  If it doesn’t print any errors, you’ve got some other issue.

quick drive-by:  look at the show event, not checking phase, will add two listeners, will call gotoScene twice

Good catch Dave. His code is also calling removeScene twice too.

Rob

How do I check the phase ?

I don’t see where I called it twice and I reviewed it multiple times

Please, try and do your own research before asking such basic questions. The docs are there for a reason! :slight_smile:

https://docs.coronalabs.com/api/library/composer/index.html

There is also a guide you should read as well:   https://docs.coronalabs.com/guide/system/composer/index.html

There is a diagram that helps explain how Composer’s events flow.

Rob

I have this code and the problem is solved :

function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("Phase started") elseif ( phase == "did" ) then print("phase showing objects") end composer.removeScene( "restart" ) end scene:addEventListener( "show" )

Thanks everyone for the help .

That doesn’t seem right.  You’re removing twice.

Probably better to do this:

function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("Phase started") elseif ( phase == "did" ) then print("phase showing objects") composer.removeScene( "restart" ) -- MOVE IT TO DID end end scene:addEventListener( "show" )

Should I do it to both scenes ?