This site requires JavaScript, please enable it in your browser!
Greenfoot back
ddemarco06
ddemarco06 wrote ...

2014/7/28

Run time error; Actor not in this world.

ddemarco06 ddemarco06

2014/7/28

#
I posted this in a early thread but I wrote the wrong topic. D'oh! So if you see that one disregard. I keep getting a run time error: actor not in this world. Any idea what I'm doing wrong Here are my methods in the laser and boss classes: Laser class method /** * Check whether we have hit the Boss. */ public void checkBossHit() { if (getWorld() == null) return; Boss boss = (Boss) getOneIntersectingObject(Boss.class); if (boss != null){ getWorld().removeObject(this); boss.hitBoss(); } } And in the boss class: public void hitBoss() { if (getWorld() == null) return; if (bossLife <=0) { Greenfoot.playSound("Explosion.wav"); Space space = (Space) getWorld(); space.addObject(new Explosion(), getX(), getY()); space.removeObject(this); Greenfoot.playSound("hooray.wav"); getWorld().removeObjects(getWorld().getObjects(Counter.class)); space.addObject(new Congratulations(), space.getWidth()/2, space.getHeight()/2); } bossLife--; } Below is the error message. Any help would be appreciated. Thank you! java.lang.NullPointerException at Boss.hitBoss(Boss.java:103) at Laser.checkBossHit(Laser.java:80) at Laser.act(Laser.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Laser.checkBossHit(Laser.java:76) at Laser.act(Laser.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Boss.hitBoss(Boss.java:103) at Laser.checkBossHit(Laser.java:80) at Laser.act(Laser.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Boss.hitBoss(Boss.java:104) at Laser.checkBossHit(Laser.java:80) at Laser.act(Laser.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Boss.hitBoss(Boss.java:104) at Laser.checkBossHit(Laser.java:80) at Laser.act(Laser.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Boss.hitBoss(Boss.java:104) at Laser.checkBossHit(Laser.java:80) at Laser.act(Laser.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Laser.checkBossHit(Laser.java:75) at Laser.act(Laser.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Laser.checkBossHit(Laser.java:75) at Laser.act(Laser.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Laser.checkBossHit(Laser.java:75) at Laser.act(Laser.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Laser.checkBossHit(Laser.java:75) at Laser.act(Laser.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Boss.hitBoss(Boss.java:104) at Laser.checkBossHit(Laser.java:80) at Laser.act(Laser.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
NikZ NikZ

2014/7/28

#
In hitBoss(), you have your actor performing actions AFTER it was deleted (nullPointer), and also in your other method.
NikZ NikZ

2014/7/28

#
When would getWorld() equal null? It can't be null as the Laser has to be in a world.
ddemarco06 ddemarco06

2014/7/28

#
If I remove the "if (getWorld() == null) return;" from the checkBossHit method I get the same run time error only it happens as soon as I shoot the laser. Which actions in hitBoss() can't be performed after I remove it?
davmac davmac

2014/7/28

#
Which actions in hitBoss() can't be performed after I remove it?
Any action which requires using the actor's position. That includes the collision checking and the getX() and getY() methods.
ddemarco06 ddemarco06

2014/7/28

#
Thanks for responding so quickly NikZ, that seemed to do the trick I rewrote my methods as such: public void hitBoss() { if (getWorld() == null) return; if (bossLife <=0) { if (getWorld() == null) return; Greenfoot.playSound("Explosion.wav"); Space space = (Space) getWorld(); space.addObject(new Explosion(), getX(), getY()); Greenfoot.playSound("hooray.wav"); getWorld().removeObjects(getWorld().getObjects(Counter.class)); space.removeObject(this); space.addObject(new Congratulations(), space.getWidth()/2, space.getHeight()/2); } bossLife--; } and /** * Check whether we have hit the Boss. */ public void checkBossHit() { if (getWorld() == null) return; Boss boss = (Boss) getOneIntersectingObject(Boss.class); if (boss != null){ getWorld().removeObject(this); boss.hitBoss(); } } However, if I remove that if (getWorld() == null) return; from the laser method I get a run time error.
NikZ NikZ

2014/7/28

#
Okay, as long as it works.
davmac davmac

2014/7/28

#
However, if I remove that if (getWorld() == null) return; from the laser method I get a run time error.
Yes, as soon as you remove an actor from the world it's getWorld() method will return null. If you try to call something using a null reference you will get a NullPointerException. So, if you have a method being called when the actor is possibly no longer in the world, you need to check for that (i.e. check if getWorld() == null) and return if so.
You need to login to post a reply.