I'm trying to delete one bush sprite at a time when it touches the side of the screen but when it hits the wall it will delete but it comes up with:
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:681)
at greenfoot.Actor.getOneIntersectingObject(Actor.java:930)
at Grass.act(Grass.java:260)
at greenfoot.core.Simulation.actActor(Simulation.java:583)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:541)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
this is my coding:
if (getWorld() instanceof Safari4){
if (counter7 == 30){
setLocation( getX() +10, getY() );
counter7 = 0;
}
counter7 ++;
if (getX()>=getWorld().getWidth()-1)
{
getWorld().removeObject(this); //removes grass at the end of the screen
}
if (counter4 == 120)
{
if(getOneIntersectingObject(Player.class) != null )//when player hits the grass
{
Actor collision = getOneIntersectingObject(Grass.class);
int rng=Greenfoot.getRandomNumber(50);
if (rng<=46 && rng >= 50 ){rng= 1 ;}
else if (rng<=40 && rng >= 45 ){rng= 2 ;}
else if (rng<=29 && rng >= 39 ){rng= 3 ;}
else if (rng<=16 && rng >= 28 ){rng= 4 ;}
else if (rng<=1 && rng >= 15 ){rng= 5 ;}
switch (rng) {
case 1:
getWorld().addObject( new Electivire(), 250, 35 ); // spawns a salamence
counter4 = 0; //sets the counter to zero
Storage.setElectivire();
System.out.println ("case:"+ rng);
break; //stops the case
case 2:
getWorld().addObject( new Rampardos(), 180, 35 ); // spawns a Tyranitar
counter4 = 0; //sets the counter to zero
Storage.setRampardos();
System.out.println ("case:"+ rng);
break; //stops the case
case 3:
getWorld().addObject( new Luxray(), 120, 35 ); // spawns a Dragonite
counter4 = 0; //sets the counter to zero
Storage.setLuxray();
System.out.println ("case:"+ rng);
break; //stops the case
case 4:
getWorld().addObject( new Bibarel(), 60, 25 ); // spawns a Donphan
counter4 = 0; //sets the counter to zero
Storage.setBibarel();
System.out.println ("case:"+ rng);
break; //stops the case
case 5:
getWorld().addObject( new Turtwig(), 20, 20 ); // spawns a turtwig
counter4 = 0; //sets the counter to zero
Storage.setTurtwig();
System.out.println ("case:"+ rng);
break; //stops the case
}
}
}
else
{
System.out.println (counter2); //for testing not going to happen in the game
counter4 ++ ; //adds one to the counter
}
}
any ideas on how to fix this?

