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

2019/10/14

Illegal State Exception

AdiBak AdiBak

2019/10/14

#
Hello, I'm trying to make my actor get removed from the screen when it crosses a world edge. However, I'm receiving an illegal state exception. Can someone please help? Thank you. Here's the exception:
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:713)
	at greenfoot.Actor.getY(Actor.java:178)
	at Bomb.act(Bomb.java:28)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)

Here's my act method:
public void act() 
    {
        // Add your action code here.
        move(3);
        if (getWorld() != null){
            if (getX() > getWorld().getWidth()){
                getWorld().removeObject(this);
            }
            if (getY() < 0){
                getWorld().removeObject(this);
            }
            if (getY() > getWorld().getHeight()){
                getWorld().removeObject(this);
            }
        }
    }    
danpost danpost

2019/10/15

#
AdiBak wrote...
I'm trying to make my actor get removed from the screen when it crosses a world edge. However, I'm receiving an illegal state exception.<< Error Trace Omitted >> << Code Omitted >>
Use else if on lines 9 and 12.
You need to login to post a reply.