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

2014/7/28

Making my bullets disappear instead of wrap around the world?

ddemarco06 ddemarco06

2014/7/28

#
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.
davmac davmac

2014/7/28

#
This line:
getWorld().removeObjects(getWorld().getObjects(Counter.class)); 
Should be:
space.removeObjects(getWorld().getObjects(Counter.class));
danpost danpost

2014/7/28

#
Correction:
space.removeObjects(space.getObjects(Counter.class));
davmac davmac

2014/7/28

#
Aha, yes :) Good catch danpost.
You need to login to post a reply.