Runtime Error: "Attempt to perform arithmetic on field 'x' (a nil value)"

I have this error,
“Attempt to perform arithmetic on field ‘x’ (a nil value) stack traceback:”

if someone can help me…

function redraw () 

Trail = display.newCircle( myCircle.x + trailprops.x, myCircle.y + trailprops.y, trailprops.r)

   Trail:setFillColor (255, 0, 0)

myCircle:toFront( )

sceneGroup:insert(Trail)

end

Runtime:addEventListener( “enterFrame”, redraw )

i also have problems on arthimetic value im new to lua and i cant seem to understand how simple addition works on values
 

btnCounter = txtNumber

txtNumber = btnCounter + 1

print( btnCounter )

thats the code but it show arithmetic error. can someone explain why and how to fix that? thanks

Hi ri_cruz,

Are you adding the enterFrame listener before the myCircle  and trailprops objects have been created?  If either of them are set to nil when the program reaches your enterFrame listener, it will fail.  Try the following:

function redraw ()  --Only execute the following if the myCircle and trailprops objects exist...   if (myCircle) and (trailprops) then     Trail = display.newCircle( myCircle.x + trailprops.x, myCircle.y + trailprops.y, trailprops.r)     Trail:setFillColor (255, 0, 0)     myCircle:toFront( )     sceneGroup:insert(Trail)   end end

If the above works, then it means you need to call Runtime:addEventListener later in your program.

Hi guiller,

I think your problem may be similar.  If txtNumber hasn’t been set to anything before you call your code, then it will fail.

--If txtNumber is nil at this point, btn Counter will be nil too. btnCounter = txtNumber --Your code will fail, because you can't add 1 to nil. txtNumber = btnCounter + 1

An easy debug test would be:

if (not txtNumber) then --If the program counter gets here, txtNumber is set to nil (ie. hasn't been set yet)   print("txtNumber hasn't been set yet!") else  --If the program counter gets here, txtNumber has something in it. btnCounter = txtNumber   txtNumber = btnCounter + 1   print( btnCounter ) end

Yes, it works, but the trail doen’t show

Hi again,

That means either myCircle or trailprops doesn’t exist when the redraw function is called.  You probably need to call Runtime:addEventListener( “enterFrame”, redraw ) later in your program.

Hi happy
I try call runtime later and the error continue appearing.

If you wanna see my code, it’s here:

local composer = require( “composer” )

local scene = composer.newScene()

local widget = require (“widget”)

local function buttonHit(event)

composer.gotoScene( event.target.destination )

return true

end

– “scene:create()”

function scene:create( event )

    local sceneGroup = self.view

    local background = display.newImage( “campo-futebol.jpg”)

sceneGroup:insert(background)

–backButton

local backBtn = display.newText(“Back”, 0, 0, “Helvetica”, 38)

backBtn.x= 640

backBtn.y= 970

backBtn.destination = “menu”

backBtn:addEventListener(“tap”, buttonHit)

composer.removeScene(true)

sceneGroup:insert(backBtn)

– Limites do ecrã

local borderTop = display.newRect( 0, 0, 707, 1 )

borderTop:setFillColor( 0, 0, 0, 0) – make invisible

physics.addBody( borderTop, “static”, borderBodyElement )

 sceneGroup:insert(borderTop)

local borderBottom = display.newRect( 0, 1000, 707, 1 )

borderBottom:setFillColor( 0, 0, 0, 0) – make invisible

physics.addBody( borderBottom, “static”, borderBodyElement )

sceneGroup:insert(borderBottom)

local borderLeft = display.newRect( 0, 1, 1, 1000 )

borderLeft:setFillColor( 0, 0, 0, 0) – make invisible

physics.addBody( borderLeft, “static”, borderBodyElement )

sceneGroup:insert(borderLeft)

local borderRight = display.newRect( 707, 1, 1, 1000 )

borderRight:setFillColor( 0, 0, 0, 0) – make invisible

physics.addBody( borderRight, “static”, borderBodyElement )

sceneGroup:insert(borderRight)

    – Initialize the scene here.

    – Example: add display objects to “sceneGroup”, add touch listeners, etc.

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).

local myCircle = display.newCircle(40, 40, 30)

physics.addBody (myCircle, “dynamic”, {density=1.0, friction=0.5, bounce=0.0 } )

myCircle:setFillColor ( 255, 0, 0)

myCircle.x = 580

myCircle.y = 435

sceneGroup:insert(myCircle)

local function circleTouch(event)

local touchedObject = event.target

–Mover circuloVermelho

if event.phase == “began” then

touchedObject.previousX = touchedObject.x

touchedObject.previousY = touchedObject.y

elseif event.phase == “moved” then

touchedObject.x = (event.x - event.xStart) + touchedObject.previousX

touchedObject.y = (event.y - event.yStart) + touchedObject.previousY

end

return true

end

myCircle:addEventListener( “touch”, circleTouch)

local trailprops = {x= 0, y = 0,r = 5} – r= tamanho do circulo do rasto

function redraw () – Desenha o rasto

Trail = display.newCircle( myCircle.x + trailprops.x, myCircle.y + trailprops.y, trailprops.r)

   Trail:setFillColor (255, 0, 0)

myCircle:toFront( )

sceneGroup:insert(Trail)

end

Runtime:addEventListener( “enterFrame”, redraw )

elseif ( phase == “did” ) then

        – Called when the scene is now on screen.

        – Insert code here to make the scene come alive.

        – Example: start timers, begin animation, play audio, etc.

    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

        – Called immediately after scene goes off screen.

    end

end

– “scene:destroy()”

function scene:destroy( event )

    local sceneGroup = self.view

    – 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

Hi again,

Your code runs fine when I tried it on my machine using the latest daily build  :slight_smile:

Maybe this is scoping problem in an earlier build of Corona?

I have this one “Build 2015.2632” and what do you have?
The error starts when I start on the menu scene and go to that one, when I want to come back to the menu scene again, it give me that error

i also have problems on arthimetic value im new to lua and i cant seem to understand how simple addition works on values
 

btnCounter = txtNumber

txtNumber = btnCounter + 1

print( btnCounter )

thats the code but it show arithmetic error. can someone explain why and how to fix that? thanks

Hi ri_cruz,

Are you adding the enterFrame listener before the myCircle  and trailprops objects have been created?  If either of them are set to nil when the program reaches your enterFrame listener, it will fail.  Try the following:

function redraw ()  --Only execute the following if the myCircle and trailprops objects exist...   if (myCircle) and (trailprops) then     Trail = display.newCircle( myCircle.x + trailprops.x, myCircle.y + trailprops.y, trailprops.r)     Trail:setFillColor (255, 0, 0)     myCircle:toFront( )     sceneGroup:insert(Trail)   end end

If the above works, then it means you need to call Runtime:addEventListener later in your program.

Hi guiller,

I think your problem may be similar.  If txtNumber hasn’t been set to anything before you call your code, then it will fail.

--If txtNumber is nil at this point, btn Counter will be nil too. btnCounter = txtNumber --Your code will fail, because you can't add 1 to nil. txtNumber = btnCounter + 1

An easy debug test would be:

if (not txtNumber) then --If the program counter gets here, txtNumber is set to nil (ie. hasn't been set yet)   print("txtNumber hasn't been set yet!") else  --If the program counter gets here, txtNumber has something in it. btnCounter = txtNumber   txtNumber = btnCounter + 1   print( btnCounter ) end

Yes, it works, but the trail doen’t show

Hi again,

That means either myCircle or trailprops doesn’t exist when the redraw function is called.  You probably need to call Runtime:addEventListener( “enterFrame”, redraw ) later in your program.

Hi happy
I try call runtime later and the error continue appearing.

If you wanna see my code, it’s here:

local composer = require( “composer” )

local scene = composer.newScene()

local widget = require (“widget”)

local function buttonHit(event)

composer.gotoScene( event.target.destination )

return true

end

– “scene:create()”

function scene:create( event )

    local sceneGroup = self.view

    local background = display.newImage( “campo-futebol.jpg”)

sceneGroup:insert(background)

–backButton

local backBtn = display.newText(“Back”, 0, 0, “Helvetica”, 38)

backBtn.x= 640

backBtn.y= 970

backBtn.destination = “menu”

backBtn:addEventListener(“tap”, buttonHit)

composer.removeScene(true)

sceneGroup:insert(backBtn)

– Limites do ecrã

local borderTop = display.newRect( 0, 0, 707, 1 )

borderTop:setFillColor( 0, 0, 0, 0) – make invisible

physics.addBody( borderTop, “static”, borderBodyElement )

 sceneGroup:insert(borderTop)

local borderBottom = display.newRect( 0, 1000, 707, 1 )

borderBottom:setFillColor( 0, 0, 0, 0) – make invisible

physics.addBody( borderBottom, “static”, borderBodyElement )

sceneGroup:insert(borderBottom)

local borderLeft = display.newRect( 0, 1, 1, 1000 )

borderLeft:setFillColor( 0, 0, 0, 0) – make invisible

physics.addBody( borderLeft, “static”, borderBodyElement )

sceneGroup:insert(borderLeft)

local borderRight = display.newRect( 707, 1, 1, 1000 )

borderRight:setFillColor( 0, 0, 0, 0) – make invisible

physics.addBody( borderRight, “static”, borderBodyElement )

sceneGroup:insert(borderRight)

    – Initialize the scene here.

    – Example: add display objects to “sceneGroup”, add touch listeners, etc.

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).

local myCircle = display.newCircle(40, 40, 30)

physics.addBody (myCircle, “dynamic”, {density=1.0, friction=0.5, bounce=0.0 } )

myCircle:setFillColor ( 255, 0, 0)

myCircle.x = 580

myCircle.y = 435

sceneGroup:insert(myCircle)

local function circleTouch(event)

local touchedObject = event.target

–Mover circuloVermelho

if event.phase == “began” then

touchedObject.previousX = touchedObject.x

touchedObject.previousY = touchedObject.y

elseif event.phase == “moved” then

touchedObject.x = (event.x - event.xStart) + touchedObject.previousX

touchedObject.y = (event.y - event.yStart) + touchedObject.previousY

end

return true

end

myCircle:addEventListener( “touch”, circleTouch)

local trailprops = {x= 0, y = 0,r = 5} – r= tamanho do circulo do rasto

function redraw () – Desenha o rasto

Trail = display.newCircle( myCircle.x + trailprops.x, myCircle.y + trailprops.y, trailprops.r)

   Trail:setFillColor (255, 0, 0)

myCircle:toFront( )

sceneGroup:insert(Trail)

end

Runtime:addEventListener( “enterFrame”, redraw )

elseif ( phase == “did” ) then

        – Called when the scene is now on screen.

        – Insert code here to make the scene come alive.

        – Example: start timers, begin animation, play audio, etc.

    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

        – Called immediately after scene goes off screen.

    end

end

– “scene:destroy()”

function scene:destroy( event )

    local sceneGroup = self.view

    – 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

Hi again,

Your code runs fine when I tried it on my machine using the latest daily build  :slight_smile:

Maybe this is scoping problem in an earlier build of Corona?

I have this one “Build 2015.2632” and what do you have?
The error starts when I start on the menu scene and go to that one, when I want to come back to the menu scene again, it give me that error