Runtime Error when changing scene

When my scene changes to my file called “golevel2”, i get this error

[code]
Runtime error
/Users/Mazensalih/Documents/penguin1/level1.lua:157: attempt to perform arithmetic on field ‘x’ (a nil value)
stack traceback:
[C]: ?
/Users/Mazensalih/Documents/penguin1/level1.lua:157: in function
?: in function <?:215>

– Below is my “golevel2” file

module(…, package.seeall)

_W = display.contentWidth;
_H = display.contentHeight;

function new()
local localGroup = display.newGroup()

bg = display.newImage(“background.png”)
localGroup:insert(bg)

next = display.newImage(“next.png”)
next.x = 240
next.y = 225
localGroup:insert(next)

nex1 = display.newText(“Next”, 100, 10, native.systemFont, 34)
nex1:setTextColor(0, 0, 0)
nex1.x = 225
nex1.y = 225

level = display.newText( “Congratulations!”, 100, 10, native.systemFont, 38)
level:setTextColor(0, 0, 0)

local function pressNext (event)
if event.phase == “ended” then
director:changeScene (“level2”)
end
end
next:addEventListener (“touch”, pressNext)

– MUST return a display.newGroup()
return localGroup
end

[import]uid: 132369 topic_id: 25491 reply_id: 325491[/import]

I am guessing what happened is : You have some display object in level1 (say Object1 ) and do some arithmetic operations on Object1.x in line 157 in level1.lua.
When changing to level2, director cleans all objects. Display Object Object1 is also deleted. So Object1 is just a table now whose x value is nil i.e Object1.x is nil
You should keep a check or something.
Something like[lua]if not object.x then return end [/lua] [import]uid: 64174 topic_id: 25491 reply_id: 102989[/import]

Thank you for replying. Can you please tell me again for what i have to do. I have a d-pad set up on lines 157. My codes for lines 157 are…
[lua]local function movepenguin (event)
penguin.x = penguin.x + motionx
end

Runtime:addEventListener(“enterFrame”, movepenguin)
– When an arrow is pushed, this will make me move.

function left:touch()
motionx = -speed
end
left:addEventListener(“touch”,left)

function right:touch()
motionx = speed
end
right:addEventListener(“touch”,right)

– The above four functions are stating the arrows should all listen for touches and defining
– the way I should move based on each touch.
[import]uid: 132369 topic_id: 25491 reply_id: 102993[/import]

Ok first you need to remove the enterframe listener when you go to level2…
Thats probably whats causing the error

Add the line
[lua] Runtime:removeEventListener(“enterFrame”, movepenguin)[/lua]
in the place where you change levels

And just in case, you can add this line to your movePenguin function
[lua]local function movepenguin (event)
if not penguin.x then return end
penguin.x = penguin.x + motionx
end[/lua]
[import]uid: 64174 topic_id: 25491 reply_id: 102994[/import]

I tried , but another problem comes up. If you can please just spend 5 minutes on my project and try to fix it for me i could send you an invite. Just please give me your email address. Also the problem is …

[code]

Runtime error
/Users/Mazensalih/Documents/penguin1/level1.lua:185: attempt to compare nil with number
stack traceback:
[C]: ?
/Users/Mazensalih/Documents/penguin1/level1.lua:185: in function
?: in function <?:215>
[import]uid: 132369 topic_id: 25491 reply_id: 102997[/import]

I could send you an invite from dropbox [import]uid: 132369 topic_id: 25491 reply_id: 102998[/import]