removeSelf() impossible nil

Hi guys, I’m new regarding Lua language but this error seems very odd to me. How could this happen?

[lua]

 if board[boardRow][boardColumn].arrow then --Line 632
           board[boardRow][boardColumn].arrow:removeSelf() --Line 633

[/lua]

main.lua:633: attempt to call method ‘removeSelf’ (a nil value)
stack traceback:
    [C]: in function ‘removeSelf’
    …s/main.lua:633: in function <…s/main.lua:604>
    ?: in function <?:229>
Program stopped (pid: 550).

The if is checking for different than null value and then why it is a null value? What happens?

Thanks!

hi rodrigo,

the ‘if/then’ is checking to see if you have an object, like a Lua table or a Corona display object (eg, image). obviously that part checks true.

this part:

 \<snip\>.arrow:removeSelf() 

is Lua attempting to 1. find property on object ‘arrow’ called ‘removeSelf’ and 2. call that property as a method.

Lua: “arrow, give me your property ‘removeSelf’”
arrow: nil (ie, i don’t have one)
Lua <calling ‘nil’>: ouch !!

the **lookup** for ‘removeSelf’ object ‘arrow’ is the issue, not that arrow doesn’t exist.

the question now is: what type of object **is** arrow, and why doesn’t it have ‘removeSelf’.

cheers,
dmc

Hi dmc, thanks a lot for replying. The type is an image, and I created the object like this:

[lua]

board[boardRow][boardColumn].arrow = display.newImageRect(“up.png”, openTileWidth,openTileHeight)

[/lua]

The point is: If I click once in this tile, it works. The second time it throws the error. So I guess in the first time it removes the object, so why there is still there an .arrow ?

Thanks a again,

Are you nilling the object after removing it? It probably isn’t nil after you remove it the first time and therefore it still thinks theres something to remove.

Try (if your not already):

board[boardRow][boardColumn].arrow:removeSelf()

board[boardRow][boardColumn].arrow = nil

TandG, you nailed it. when I add the = nil the problem is gone. I knew it was missing something :slight_smile:

Thanks a lot!

hi rodrigo,

i’m glad you found the issue.

the thing with Corona display objects, like image, is they are a hybrid object given aspects from Lua and the underlying C++. the outer “wrapper” of these objects is indeed a plan Lua table, but it also contains properties and methods which are created/used by the C++ side.

when calling removeSelf() on an object, Corona will “clean up” the parts of the Lua table from the C++ side, essentially leaving it like an empty shell. you just need to remember not to use the object again, so setting your reference to ‘nil’ is exactly what is needed.

cheers,
dmc

hi rodrigo,

the ‘if/then’ is checking to see if you have an object, like a Lua table or a Corona display object (eg, image). obviously that part checks true.

this part:

 \<snip\>.arrow:removeSelf() 

is Lua attempting to 1. find property on object ‘arrow’ called ‘removeSelf’ and 2. call that property as a method.

Lua: “arrow, give me your property ‘removeSelf’”
arrow: nil (ie, i don’t have one)
Lua <calling ‘nil’>: ouch !!

the **lookup** for ‘removeSelf’ object ‘arrow’ is the issue, not that arrow doesn’t exist.

the question now is: what type of object **is** arrow, and why doesn’t it have ‘removeSelf’.

cheers,
dmc

Hi dmc, thanks a lot for replying. The type is an image, and I created the object like this:

[lua]

board[boardRow][boardColumn].arrow = display.newImageRect(“up.png”, openTileWidth,openTileHeight)

[/lua]

The point is: If I click once in this tile, it works. The second time it throws the error. So I guess in the first time it removes the object, so why there is still there an .arrow ?

Thanks a again,

Are you nilling the object after removing it? It probably isn’t nil after you remove it the first time and therefore it still thinks theres something to remove.

Try (if your not already):

board[boardRow][boardColumn].arrow:removeSelf()

board[boardRow][boardColumn].arrow = nil

TandG, you nailed it. when I add the = nil the problem is gone. I knew it was missing something :slight_smile:

Thanks a lot!

hi rodrigo,

i’m glad you found the issue.

the thing with Corona display objects, like image, is they are a hybrid object given aspects from Lua and the underlying C++. the outer “wrapper” of these objects is indeed a plan Lua table, but it also contains properties and methods which are created/used by the C++ side.

when calling removeSelf() on an object, Corona will “clean up” the parts of the Lua table from the C++ side, essentially leaving it like an empty shell. you just need to remember not to use the object again, so setting your reference to ‘nil’ is exactly what is needed.

cheers,
dmc