RemoveSelf() causing error

I am using removeSelf() of a number of display objects, and it works properly, but gives me this error shown at the bottom of the code.

 local dug = display.newImage("images/dugOut.png", 0, 0)   
 --physics.addBody( dug, "static", { friction=0.6 } )   
 dug.alpha = 0.5254901960784314   
 dug.collisionType = "dug"  
 dug.timesHit = 0  
 dug.points = 0  
  
  
  
 hbLeft = display.newImage("images/hitBorder.png", 120, 0)   
 physics.addBody( hbLeft, "static", { friction=0.6 } )   
 hbLeft.collisionType = "dug"  
  
 hb1 = display.newImage("images/hitBorder.png", 28, 0)   
 physics.addBody( hb1, "static", { friction=0.6 } )   
 hb1.collisionType = "dug"  
  
 hb2 = display.newImage("images/hitBorder.png", 120, 0)   
 physics.addBody( hb2, "static", { friction=0.6 } )   
 hb2.collisionType = "dug"  
  
 hb3 = display.newImage("images/hitBorder.png", 360, 0)   
 physics.addBody( hb3, "static", { friction=0.6 } )   
 hb3.collisionType = "dug"  
  
 hb4 = display.newImage("images/hitBorder.png", 450, 0)   
 physics.addBody( hb4, "static", { friction=0.6 } )   
 hb4.collisionType = "dug"  
  
 local function removeSetup()  
 dug:removeSelf()  
 hb1:removeSelf()  
 hb2:removeSelf()  
 hb3:removeSelf()  
 hb4:removeSelf()  
 buttonSmall:removeSelf()  
  
 end  
  
  
 buttonHandler = function( event )  
 --t:setText( "id = " .. event.id .. ", phase = " .. event.phase )  
 timer.performWithDelay(1000, removeSetup)  
 --dug:removeSelf()  
 --hb1:removeSelf()  
 --hb2:removeSelf()  
 --hb3:removeSelf()  
 --hb4:removeSelf()  
 --t = event.target  
 --t:removeSelf()  
 end  
  
 buttonSmall = ui.newButton{  
 default = "images/buttonBlueSmall.png",  
 over = "images/buttonBlueSmallOver.png",  
 onEvent = buttonHandler,  
 id = "smallBtn",  
 text = "Fight",  
 size = 12,  
 emboss=true  
 }  
 buttonSmall.x = 240; buttonSmall.y = 160  
  

Output Error

Copyright (C) 2009-2010 A n s c a , I n c . Version: 2.0.0 Build: 2010.243 The file sandbox for this project is located at the following folder: (/Users/derwydd/Library/Application Support/Corona Simulator/game3-2188ABD515A7F5EC2C73D852830661CB) Runtime error ...kiiClover/Capture the Flag/game3/sticksAndStones.lua:35: attempt to call method 'removeSelf' (a nil value) stack traceback: [C]: in function 'removeSelf' ...kiiClover/Capture the Flag/game3/sticksAndStones.lua:35: in function '\_listener' ?: in function <?:441> ?: in function <?:214> [import]uid: 7197 topic_id: 4894 reply_id: 304894[/import]

I ran into the same problem. Never really figured out why it was happening, but my assumption is that it was trying to remove itself twice, so the second time it was already a nil value. This is what I ended up using:

[lua]local function remove( display )
return display:removeSelf()
end

local object = display.newImage(“myImage.png”)

local itemWorked, result = pcall(remove, object) – pcall is a native lua method, Corona supports it

if itemWorked then
print(result) – true
else
print(result) – false, this fires if it fails to remove
end[/lua] [import]uid: 12405 topic_id: 4894 reply_id: 15854[/import]

put a [lua]print(event.phase)[/lua] statement in your button handler. my guess is it’s firing more than once, ie for the “began” and the “ended” phases. since you’re not checking which, it’ll remove everything in the first phase, and fail when it tries to remove them in the second because the objects have already been removed
[import]uid: 6645 topic_id: 4894 reply_id: 15958[/import]

When you have a set of objects you want to dynamically remove, put them in a table. Then when you need to remove them, you can just loop through the table and then reset the table. This will keep you from having this error message because it won’t try to remove objects that were previously removed even if you call it twice in a row. You can also easily check to see if any items in the table exist by checking the table.maxn value. [import]uid: 8776 topic_id: 4894 reply_id: 15999[/import]

You can also try:

[blockcode]
if object.remove then
object:removeSelf()
end
[/blockcode]

It’ll check to see if there’s a remove function attached to that object, and if there is, it’ll call removeSelf(). [import]uid: 52430 topic_id: 4894 reply_id: 32215[/import]