removeSelf() returns 'nil' when dealing with spawned objects

Hello everybody, I am newer to Corona, and I’ve recently been messing around with some spawning and collision code. The issue that I am having is that when I try to remove the spawned objects upon collision, the object:removeSelf() returns the value of ‘nil’. Two different tries at this code are posted below and I get the same thing for both. 

--Spawning multiple bananas in random locations local function spawnObjects() local bananas = display.newImageRect("object\_bananas.png", 30, 20); physics.addBody(bananas, "dynamic", {radius = 11}); bananas.x = math.random(0, 320); bananas.y = -40; transition.to( bananas, {time = math.random(6000, 10000), x = math.random(10, 310) , y = 600, }); -- function to handle the collision on the bananas function bananas:collision(e) -- only perform logic when the bananas are colliding with monkey if (e.other.class == "monkey") then -- cannot remove objects during a collision, so wait a short moment for it to end timer.performWithDelay(1, function() -- remove the bananas bananas:removeSelf() end, 1) end -- always return true because we have handled the collision return true end -- attach a collision listener to the bananas bananas:addEventListener("collision",bananas) return bananas end local total\_bananas = 15 tmr = timer.performWithDelay(2000, spawnObjects, total\_bananas); ALTERNATIVE local bananas -- function to handle the collision on the bananas local function onCollision(bananas, event) -- only perform logic when the bananas are colliding with monkey if (event.other.class == "monkey") then -- cannot remove objects during a collision, so wait a short moment for it to end timer.performWithDelay(1, function() -- remove the bananas bananas:removeSelf() end, 1) end -- always return true because we have handled the collision return true end --Spawning multiple bananas in random locations local function spawnObjects() local bananas = display.newImageRect("object\_bananas.png", 30, 20); physics.addBody(bananas, "dynamic", {radius = 11}); bananas.x = math.random(0, 320); bananas.y = -40; transition.to( bananas, {time = math.random(6000, 10000), x = math.random(10, 310) , y = 600, }); bananas.collision = onCollision bananas:addEventListener("collision", bananas) sceneGroup:insert(bananas) return bananas end

Try this:

function bananas:collision(e) if (e.other.class == "monkey") then display.remove( self ) end return true end

Thank you, my friend. That code definitely worked for me, though I did come across something strange. The object was removed before the physics and display objects came into contact with something else. This mainly happened when the objects were colliding from the top and bottom. Yet, when they collided from the sides, the other object was properly destroyed during actual collision. I fixed the top and bottom collisions only by adjusting the size of the physics body, though this caused the sideways collision to be affected.  

Try this:

function bananas:collision(e) if (e.other.class == "monkey") then display.remove( self ) end return true end

Thank you, my friend. That code definitely worked for me, though I did come across something strange. The object was removed before the physics and display objects came into contact with something else. This mainly happened when the objects were colliding from the top and bottom. Yet, when they collided from the sides, the other object was properly destroyed during actual collision. I fixed the top and bottom collisions only by adjusting the size of the physics body, though this caused the sideways collision to be affected.