attempt to call method 'removeSelf' (a nil value) help?

the error only occurred after the second time I hit “play again” (local play - at line 100 on view3). it also does that on a friends simulator.

and yeh, I have looked at your code, I tried it and had a few errors that I probably could have resolved had I been patient, however I have my parents nagging at me that I will never make any money from apps so I decided to take what i wrote, which works, and finish it… so i can get the app up ASAP to (hopefully) prove my parents wrong. i think the errors i had were something to do with storyboards but i will look at that after i get this on an app store lol

i will have to look at this again tomorrow morning as it’s too late to program now :L

thanx again for trying to help :slight_smile:

good luck mate.
trying to make a point in a discussion with your parents should have been the first thing you should have mentioned here.

that’s kind of motivating :wink:

 

I managed to fix this myself :D… well not fix as much as find an alternative

instead of creating a new scene I just make the game display “time up” and the play again image over the game.

I think my errors might have had something to do with memory… it’s the only thing I can think of that changes each time the game runs

quick guess,
you are calling 
[lua]
Dtime:removeSelf()
[/lua]
before Dtime is defined as a display object,
you forward declared it, thats correct, but till the line
[lua]
Dtime = display.newText("Time: "… timed, 0, 0, native.systemFont, 14)
[/lua]
appears it’s just some kind of placeholder table that makes it available throughout your whole script whenever it got defined, that means it’s not a display object yet. it actually is nothing, or better: nil.
removeSelf() is a method inherited from the display object. means, a simple table doesnt know this method, means the method is nil. or in your output window’s words:
attempt to call method ‘removeSelf’ (a nil value)

quickest fix without making you to reorder all your script would be
[lua]
if(Dtime)then Dtime:removeSelf()end

Dtime = display.newText("Time: "… timed, 0, 0, native.systemFont, 14)

Dtime.x = display.contentWidth -30
[/lua]

this would simply make sure Dtime is not nil before calling removeSelf, but it actually doesnt check if Dtime is a display object, so you would get the same error as before if you’d be funny enough to decide to change Dtime into a table instead of a display object.

what i actually dont get is why you are removing and recreating Dtime all the time. i mean, its absolutely not wrong and will work, but it seems so unnecessary. why dont you define DTime as display.newText ONCE in your games init and just fill it with new text in your timer function. in my mind that would be the way slicker solution and not such a potential error causer.

that didn’t work, I am making it a display object am I not?

in init when I say

Dtime = display.newText("Time: ".. timed, 0, 0, native.systemFont, 14)     Dtime.x = display.contentWidth -30  

and then

group:insert(Dtime)

another thing that really confused me, is that every time I hit the play again button in view3, it is running the same code. I cant wrap my head around why it will work the first time I hit play again but not the second :confused:

any other ideas? and thanks for trying

oh wait a second, as said it was just a quick guess, i didnt read your whole code.

first: strange that it didnt work. really.
as i tested it with a codesnippet here and it should be working.
you changed it at the line 116 (116 here on your post, might be a different one in your file, i guess)?
by the way, it would be cool next time if the line numbers of your posted script would be the same as in your file, as i wouldnt have to search the line the error output is talking about by myself.

then another thing,
put the following line away from where it is right now and insert it at the bottom of your init (but that wont change a thing, but seriously would improve your file being more sorted)

  • timer.performWithDelay(1000, countdown, 7)

last but not least, could you rerun the code and post the actual line the output is complaining about.

I think I tried to fix it after I copied the stack trace but before I copied the code which is why the numbers were out… here is the code

----------------------------------------------------------------------------------------- -- -- view2.lua -- ----------------------------------------------------------------------------------------- print("miltitouch activated") system.activate( "multitouch" ) print("declerations") local storyboard = require( "storyboard" ) local scene = storyboard.newScene() storyboard.disableAutoPurge = false local pop = audio.loadStream('pop.mp3') local unPop = audio.loadStream('unpop.mp3') --local loading = audio.loadStream('load.mp3') local bubble = {} local timed local score local group local Dscore local Dtime ----------------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION -- -- NOTE: Code outside of listener functions (below) will only be executed once, -- unless storyboard.removeScene() is called. -- local Dscore ----------------------------------------------------------------------------------------- print("implementations") -- Called when the scene's view does not exist: function scene:createScene( event ) print("scene:createScene") group = self.view print("group = self.view") init() ------------------------------------------------------------------------------------------- end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) --local group = self.view print("enterScene") -- do nothing end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view print("ExitScene") -- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.) end -- If scene's view is removed, scene:destroyScene() will be called just prior to: function scene:destroyScene( event ) print("ERROR: DESTROYING SCENE") -- Dscore = null --Dtime = null --scene:removeSelf() -- INSERT code here (e.g. remove listeners, remove widgets, save state variables, etc.) end function bubbleTap( event ) if timed \>0 then print("popping") local oldBubble = event.target local count = oldBubble.index audio.rewind(pop) audio.play(pop) bubble[count] = display.newImage( 'bubbleDown.png' ) bubble[count].x = oldBubble.x bubble[count].y = oldBubble.y bubble[count].index = count --bubble[Count]:addEventListener("tap", bubbleTap) scene.view:insert(bubble[count]) updateScore() oldBubble:removeSelf() if timed \> 1 then timer.performWithDelay(1500, function() bubbleUnPop(bubble[count]) end) end end end function bubbleUnPop( event ) print("unpopping") local oldBubble = event print("bubble"..oldBubble.index) local count = oldBubble.index audio.rewind(unPop) audio.play(unPop) bubble[count] = display.newImage( 'bubble.png' ) bubble[count].x = oldBubble.x bubble[count].y = oldBubble.y bubble[count].index = count timer.performWithDelay( 100, function() bubble[count]:addEventListener("tap", bubbleTap) end, 1) scene.view:insert(bubble[count]) oldBubble:removeSelf() end function countdown () print("counting down - at: "..timed) print("using clock: ") print(Dtime) if timed == 0 then gameEnd() else timed = timed -1 if(Dtime)then Dtime:removeSelf() end Dtime = display.newText("Time: ".. timed, 0, 0, native.systemFont, 14) Dtime.x = display.contentWidth -30 scene.view:insert(Dtime) end end function updateScore() score = score+1 Dscore:removeSelf() Dscore = display.newText("Score: ".. score, 0, 0, native.systemFont, 14) end function gameEnd( event ) Dscore:removeSelf() Dtime:removeSelf() print("------------------------------------------------------------------------stats-------------------------------------------------------") print(Dscore) print(Dtime) print("-------------------------------------------------------------------------------------------------------------------------------") timer.performWithDelay(100, function() storyboard.gotoScene( "view3", {params = {scored = score}}) end) end function init() print("initialising....") print(Dscore) score = 0 timed = 6 -- create a white background to fill screen print("creating background") local bg = display.newImage('bgG.png' ) print("creating UIBar") local titleBar = display.newRect(0,0,display.contentWidth,20) titleBar:setFillColor(75,0,0) --create the score print("creating Dscore with score of: "..score) Dscore = display.newText("Score: ".. score, 0, 0, native.systemFont, 14) --create the time print("creating Dtime with time of: "..timed) Dtime = display.newText("Time: ".. timed, 0, 0, native.systemFont, 14) Dtime.x = display.contentWidth -30 -- all objects must be added to group (e.g. self.view) group:insert( bg ) print("background inserted") group:insert( titleBar ) print("UI inserted") group:insert( Dtime ) print("Dtime inserted") group:insert( Dscore ) print("Dscore inserted") -------------------------------create the bubbles------------------------------------------ local count = 1 --audio.rewind(load) --audio.play(load) for i=9,display.contentWidth,19 do--bubble horizontal for j=30,display.contentHeight,19 do--bubble verticle print("creating new bubble"..count) bubble[count] = display.newImage('bubble.png' ) bubble[count].index = count bubble[count].x = i bubble[count].y = j group:insert( bubble[count] ) bubble[count]:addEventListener("tap", bubbleTap) count = count+1 end end timer.performWithDelay(1000, countdown, 7) end function reInit() print("reinitialising...") -- storyboard.reloadScene() init() end ----------------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION ----------------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched whenever before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) ----------------------------------------------------------------------------------------- return scene

and the error is being caused at

Dtime:removeSelf()

thnx for helping, I’ve been working at this all day which for me has now been 12 solid hrs :confused: lol

ok, i copied your whole code and i did the change on the line as i suggested (and as you did as well) and i am not getting the error anymore.

but what i did too was giving the file it’s right order what would be building it from bottom up,

so, i put all your scene events to the bottom, just right above --END OF YOUR IMPLEMENTATION

but i doubt thats causing the error as NONE of your functions is local. so your lua file actually should give a crap about its order. but it makes it hard to read, hard to debug, and sooner or later leading this game into a world of pain and errors. but thats just my personal opinion.

did you even had a look at the files i especially made for you yesterday?
https://github.com/juicefoozle/bubblewrap

sorry mate, i dont want to sound rude, i am just tired. and you are posting in the newbie section, so its absolutely ok to have a messy file and to make your own mistakes with your own file instead of taking the one provided by me…

 

the error only occurred after the second time I hit “play again” (local play - at line 100 on view3). it also does that on a friends simulator.

and yeh, I have looked at your code, I tried it and had a few errors that I probably could have resolved had I been patient, however I have my parents nagging at me that I will never make any money from apps so I decided to take what i wrote, which works, and finish it… so i can get the app up ASAP to (hopefully) prove my parents wrong. i think the errors i had were something to do with storyboards but i will look at that after i get this on an app store lol

i will have to look at this again tomorrow morning as it’s too late to program now :L

thanx again for trying to help :slight_smile:

good luck mate.
trying to make a point in a discussion with your parents should have been the first thing you should have mentioned here.

that’s kind of motivating :wink:

 

I managed to fix this myself :D… well not fix as much as find an alternative

instead of creating a new scene I just make the game display “time up” and the play again image over the game.

I think my errors might have had something to do with memory… it’s the only thing I can think of that changes each time the game runs

I have this same bug all over the place, with insert apparently being nil and remove and removeSelf

It’s painful as hell, does anyone have a fix for this yet?

tried asserting whether the table or the object with insert and remove were nil already and they weren’t. Also this is an error which only occurs on the second time I load a scene, the first time runs without hitches, but I really dont see how anything could cause those functions to nil themselves, I certainly don’t do it anywhere.

Heres an example of one of the errors

attempt to call method 'insert' (a nil value)

local rect = display.newRect(0,0,game.screen.width,game.screen.height) rect:setFillColor(100, 100, 100, 150) group:insert(rect)

Any help would be vastly appreciated since this is to a deadline and this bug has been re-occuring throughout wherever I reload a scene

At one level, the issue with objects not being able to removeSelf is usually caused by the object already having been removed from the display somehow.

One sure fire way to avoid the error is to test for the presence of removeSelf() as a method *before* calling it. This can be done as so

[lua]

    if( rect.removeSelf ~= nil ) then    – Make sure it is still in the display hierarchy

        rect:removeSelf()

    end

[/lua]

However, this does not solve any fundamental issues code may have with symmetrical loading / releasing of assets. It’s just a method to check that objects actually are in the display hierarchy before deleting them. (The removeSelf method is added to the object when inserted into the display, and the mthod is removed when the object is removed from the display).

Don’t think I’ve every seen this trick posted in the forums, hopefully a corona staffer won’t jump in here and blast me out of the water on this technique… But it seems to work as a last ditch stopgap.

Thanks I’ll try that soon and get back to you, the only fix I can work out is just to embed the game code at the main.lua level then it never reloads just art assets, but eventually I’ve found that using :translate or .x on a sprite begins to break down in the same manner.

It’s a really frustrating bug that seems to have nothing with my code causing it.

Your case sounds tougher obscure.

As a general practice, whenever I create an object in code, I write (at the same time) the code/function to remove it. By doing it at the same time (literally), I’m (forced to be) aware of any/all the special circumstances that might happen to the object between the time it is created and destroyed.

If you’ve just been creating objects left and right, and inserting/removing them arbitrarily - then it could be difficult to track down the sources of your errors.

In your example just above, you didn’t show how the group was created. As is, that code should work. However, you say it doesn’t so we must assume there’s more to it than that. My guess would be (in that particular case) that group cannot insert the object because group itself was somehow removed from the display (if it’s parent was removed, that counts too).

I have this same bug all over the place, with insert apparently being nil and remove and removeSelf

It’s painful as hell, does anyone have a fix for this yet?

tried asserting whether the table or the object with insert and remove were nil already and they weren’t. Also this is an error which only occurs on the second time I load a scene, the first time runs without hitches, but I really dont see how anything could cause those functions to nil themselves, I certainly don’t do it anywhere.

Heres an example of one of the errors

attempt to call method 'insert' (a nil value)

local rect = display.newRect(0,0,game.screen.width,game.screen.height) rect:setFillColor(100, 100, 100, 150) group:insert(rect)

Any help would be vastly appreciated since this is to a deadline and this bug has been re-occuring throughout wherever I reload a scene

At one level, the issue with objects not being able to removeSelf is usually caused by the object already having been removed from the display somehow.

One sure fire way to avoid the error is to test for the presence of removeSelf() as a method *before* calling it. This can be done as so

[lua]

    if( rect.removeSelf ~= nil ) then    – Make sure it is still in the display hierarchy

        rect:removeSelf()

    end

[/lua]

However, this does not solve any fundamental issues code may have with symmetrical loading / releasing of assets. It’s just a method to check that objects actually are in the display hierarchy before deleting them. (The removeSelf method is added to the object when inserted into the display, and the mthod is removed when the object is removed from the display).

Don’t think I’ve every seen this trick posted in the forums, hopefully a corona staffer won’t jump in here and blast me out of the water on this technique… But it seems to work as a last ditch stopgap.

Thanks I’ll try that soon and get back to you, the only fix I can work out is just to embed the game code at the main.lua level then it never reloads just art assets, but eventually I’ve found that using :translate or .x on a sprite begins to break down in the same manner.

It’s a really frustrating bug that seems to have nothing with my code causing it.

Your case sounds tougher obscure.

As a general practice, whenever I create an object in code, I write (at the same time) the code/function to remove it. By doing it at the same time (literally), I’m (forced to be) aware of any/all the special circumstances that might happen to the object between the time it is created and destroyed.

If you’ve just been creating objects left and right, and inserting/removing them arbitrarily - then it could be difficult to track down the sources of your errors.

In your example just above, you didn’t show how the group was created. As is, that code should work. However, you say it doesn’t so we must assume there’s more to it than that. My guess would be (in that particular case) that group cannot insert the object because group itself was somehow removed from the display (if it’s parent was removed, that counts too).