Runtime Error Display Image Nil

On line 81, I get the following error:  

/Users/daniel430lee/Desktop/sideScroller/game.lua:81: attempt to index global ‘city1’ (a nil value)

stack traceback:

    [C]: ?

    /Users/daniel430lee/Desktop/sideScroller/game.lua:81: in function </Users/daniel430lee/Desktop/sideScroller/game.lua:77>

    ?: in function ‘dispatchEvent’

    ?: in function <?:1076>

    (tail call): ?

    ?: in function <?:172>

    ?: in function <?:218>

The cause of the error, I think, could be that a statement is missing that connects the graphic image files in the create-scene function and the image variables in the enter-scene function. But, I’m not so sure about that and ,even if it was, I don’t know how to fix it.  

--Requires local physics = require "physics" physics.start() local storyboard = require ("storyboard") local scene = storyboard.newScene() --Background function scene:createScene(event) local screenGroup = self.view local background = display.newImage("bg.png") local city1 = display.newImage("city1.png") city1:setReferencePoint(display.BottomLeftReferencePoint) city1.x = 0 city1.y = 320 city1.speed = 1 local city2 = display.newImage("city1.png") city2:setReferencePoint(display.BottomLeftReferencePoint) city2.x = 480 city2.y = 320 city2.speed = 1 local city3 = display.newImage("city2.png") city3:setReferencePoint(display.BottomLeftReferencePoint) city3.x = 0 city3.y = 320 city3.speed = 2 local city4 = display.newImage("city2.png") city4:setReferencePoint(display.BottomLeftReferencePoint) city4.x = 480 city4.y = 320 city4.speed = 2 local jet = display.newImage("redJet.png") jet.x = 100 jet.y = 100 physics.addBody(jet, "dynamic", {density=.1, bounce=0.1, friction=.2, radius=12}) end function scrollCity(self,event) if self.x \< -477 then self.x = 480 else self.x = self.x - self.speed end end function activateJets(self,event) self:applyForce(0, -1.5, self.x, self.y) end function touchScreen(event) if event.phase == "began" then jet.enterFrame = activateJets Runtime:addEventListener("enterFrame",jet) end if event.phase == "moved" then end if event.phase == "ended" then Runtime:removeEventListener("enterFrame",jet) end end function scene:enterScene(event) Runtime:addEventListener("touch", touchScreen) city1.enterFrame = scrollCity Runtime:addEventListener("enterFrame",city1) city2.enterFrame = scrollCity Runtime:addEventListener("enterFrame",city2) city3.enterFrame = scrollCity Runtime:addEventListener("enterFrame",city3) city4.enterFrame = scrollCity Runtime:addEventListener("enterFrame",city4) -- jet --Static jet that doesn't have animation local jet = display.newImage("redJet.png") jet.x = 100 jet.y = 100 physics.addBody(jet, "dynamic", {density=.1, bounce=0.1, friction=.2, radius=12}) end function scene:exitScene(event) end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

Hi Daniel,

Yes, your suspicion is right.  The issue is that you’ve declared your city variables as local within the createScene function.  The local keyword means the variable is only accessible within the block of code where it was declared, which is the createScene function in this case.

To fix it, you could declare your city variables as local outside of any function, which would make it accessible anywhere in the module.  For example, after line 7, put [lua]local city1[/lua] and similar for the other variables.  Then, in createScene, you can remove the local keyword, since you’ve already declared city1 as local to the entire module.

As a separate matter, instead of creating four variables city1, city2, city3, and city4, you’ll probably be better off creating a table called cities and use it as an array, like cities[1], cities[2], cities[3], and cities[4].  This gives you the flexibility to add more cities later.

  • Andrew

You have what is known as a “scope” issue.  You define city1 in createScene() by making it local.  That variable is only available to that function.  To fix, put:

local city1

near the top outside of any function then take the “local” off if it in createScene.  You will have to do this for any varialble/object that needs to be seen in both functions.

Hi Daniel,

Yes, your suspicion is right.  The issue is that you’ve declared your city variables as local within the createScene function.  The local keyword means the variable is only accessible within the block of code where it was declared, which is the createScene function in this case.

To fix it, you could declare your city variables as local outside of any function, which would make it accessible anywhere in the module.  For example, after line 7, put [lua]local city1[/lua] and similar for the other variables.  Then, in createScene, you can remove the local keyword, since you’ve already declared city1 as local to the entire module.

As a separate matter, instead of creating four variables city1, city2, city3, and city4, you’ll probably be better off creating a table called cities and use it as an array, like cities[1], cities[2], cities[3], and cities[4].  This gives you the flexibility to add more cities later.

  • Andrew

You have what is known as a “scope” issue.  You define city1 in createScene() by making it local.  That variable is only available to that function.  To fix, put:

local city1

near the top outside of any function then take the “local” off if it in createScene.  You will have to do this for any varialble/object that needs to be seen in both functions.