Attempt to call insert (a nil value)

Hey guys this error has been bothering me for awhile now. Does anybody have any idea why it is happening so  I get this error when the scene changes.

Thanks Tyler Jacobson

local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view --composer.recycleOnSceneChange = true local BG = display.newImage ("BG.png") sceneGroup:insert(BG) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then local group = display.newGroup() local smile composer.recycleOnSceneChange = true local physics = require("physics") physics.start() local Gem = display.newImage("Gem.png", 50, 50) Gem.x = display.contentCenterX Gem.y = display.contentCenterY physics.addBody(Gem) Gem.isSensor = false local numberSmiles = 1 --local variable; amount can be changed sceneGroup:insert(Gem) physics.setGravity(0,0) physics.setDrawMode( "hybrid" ) local function spawnTop() for i=1,numberSmiles do local smile = display.newImage("Spear.png", math.random(-10, 400),-200 ); --smile:scale(0.02,0.02) physics.addBody( smile,"static", { density=0, friction=0, bounce=0 } ) smile.x = display.contentCenterX smile:rotate(180) -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default smile.isFixedRotation = true transition.to( smile, { time=3000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1000,2000), spawnTop, -1 ) --fire every 10 seconds local numberSmiles = 1 --local variable; amount can be changed -- local function spawnBottom() for i=1,numberSmiles do local smile = display.newImage("Spear.png", math.random(-10, 400),700 ); --smile.x = display.contentCenterX smile:scale(0.02,0.02) physics.addBody( smile, "static",{ density=0, friction=0, bounce=0 } ) -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default smile.isFixedRotation = true transition.to( smile, { time=3000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(2000,3000), spawnBottom, -1 ) --fire every 10 seconds local numberSmiles = 1 --local variable; amount can be changed local function spawnLeft() for i=1,numberSmiles do local smile = display.newImage("Spear.png", -50,math.random(0,500), 500 ); smile.y = display.contentCenterY --smile:scale(0.02,0.02) smile:rotate(90) -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default physics.addBody( smile, "static",{ density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=1500 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1500,2500), spawnLeft, -1 ) --fire every 10 seconds local numberSmiles = 1 --local variable; amount can be changed local function spawnRight() for i=1,numberSmiles do local smile = display.newImage("Spear.png", 500,math.random(0,400) ); smile.y = display. contentCenterY --smile:scale(0.02,0.02) smile:rotate(-90) --smile:rotate(gembehind.radius) -- smile:setReferencePoint(); --not necessary; center is default physics.addBody( smile, "static",{ density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=2000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) print("No error please!") sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1700,2700), spawnRight, -1 ) --fire every 10 seconds local function spawnTopRight() for i=1,numberSmiles do local smile = display.newImage("Spear.png", 328,-60); --smile:scale(0.02,0.02) smile:rotate(213) --smile:rotate(gembehind.radius) -- smile:setReferencePoint(); --not necessary; center is default physics.addBody( smile, "static",{ density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=2000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1500,2500), spawnTopRight, -1 ) --fire every 10 seconds local function spawnBottomRight() for i=1,numberSmiles do local smile = display.newImage("Spear.png", 335,550 ); --smile:scale(0.02,0.02) smile:rotate(330) --smile:rotate(gembehind.radius) -- smile:setReferencePoint(); --not necessary; center is default physics.addBody( smile,"static", { density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=2000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1700,2700), spawnBottomRight, -1 ) --fire every 10 seconds local function spawnBottomLeft() for i=1,numberSmiles do local smile = display.newImage("Spear.png", -20,560 ); --smile:scale(0.02,0.02) smile:rotate(32) --smile:rotate(gembehind.radius) -- smile:setReferencePoint(); --not necessary; center is default physics.addBody( smile,"static", { density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=2000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1500,2500), spawnBottomLeft, -1 ) --fire every 10 seconds local function spawnTopLeft() for i=1,numberSmiles do local smile = display.newImage("Spear.png", -40,-120 ); --smile:scale(0.02,0.02) smile:rotate(150) --smile:rotate(gembehind.radius) -- smile:setReferencePoint(); --not necessary; center is default physics.addBody( smile, "staic",{ density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=2000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) local function onCollision(self,event ) print("changing scenes!") composer.gotoScene("GameOver") timer.cancel(timer1) end Gem:addEventListener("collision",onCollision) end end local timer1 = timer.performWithDelay( math.random(1700,2700), spawnTopLeft, -1 ) --fire every 10 seconds physics.addBody(Gem,"dynamic") end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view Runtime:removeEventListener( "collision", onCollision ) -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

TPJ,

There are several places in your code where you are inserting a display image into sceneGroup.  If one of those display images failed to find the image file it is trying to insert it will then be inserting a nil value(no image); thus the error.  

Look at the filenames such as : “Spear.png”,  “BG.png”  and make sure they are spelled exactly the same in the code as you have them named in the resource file. Make sure they are in the same folder as your main.lua  … otherwise if you have them in a subfolder like ‘Images’ or such, you would need to add that folder name to the path filename, for example: “Images/BG.png”.

There could be some other reason for the error, but often a typographical error.   Check the error message and see if there is not a line number of the error.  That will help you narrow it down.  Otherwise you just need to check each one of those ‘display.newImage’ functions to see that the filename is correct.  They must be spelled exactly the same and match lowercase or uppercase as well.

Good luck

Bob

TPJ,

There are several places in your code where you are inserting a display image into sceneGroup.  If one of those display images failed to find the image file it is trying to insert it will then be inserting a nil value(no image); thus the error.  

Look at the filenames such as : “Spear.png”,  “BG.png”  and make sure they are spelled exactly the same in the code as you have them named in the resource file. Make sure they are in the same folder as your main.lua  … otherwise if you have them in a subfolder like ‘Images’ or such, you would need to add that folder name to the path filename, for example: “Images/BG.png”.

There could be some other reason for the error, but often a typographical error.   Check the error message and see if there is not a line number of the error.  That will help you narrow it down.  Otherwise you just need to check each one of those ‘display.newImage’ functions to see that the filename is correct.  They must be spelled exactly the same and match lowercase or uppercase as well.

Good luck

Bob