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

2011/12/15

How to solve it: java.lang.IllegalStateException: Actor not in world.

b321234 b321234

2011/12/15

#
Hi guys, it's me again (sry 'bout that lol) Well I have a bullet class and when I try to run the program it says 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:656) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:860) //.............. //(Some more codes here, if I paste them all it'll be very confusing so..) So now I have a bullet class, so it's basically like... when I press space a bullet comes out. Everything works well until the bullet reaches the edge of the window OR an object (another actor. A zombie, for example) It says 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. I tried to find the error in the internet, it says this error would be thrown if there's something wrong with the getX method. Do I need to add something before that? Actually I think I need to post my bullet codes too.. public void act() { setLocation(.getX(),getY()-10); checkEdge(getY()); checkKill(); } public void checkEdge(int y){ if (y < getWorld().getHeight()-598) { getWorld().removeObject(this); } } public void checkKill() { Actor zombie; zombie = getOneObjectAtOffset(0,0,Zombie.class); if(zombie!=null) { World world; world = getWorld(); world.removeObject(zombie); Greenfoot.playSound("zombieDeath.wav"); } } Hope it's not too confusing lol... Thanks a lot guys ;)
danpost danpost

2011/12/15

#
Have checkEdge return a boolean to let you know if it has been removed from the world and skip the following statement if it has.
boolean removed = checkEdge(getY());
if (!removed) checkKill();
davmac davmac

2011/12/15

#
To explain why you are getting the problem: your checkEdge() method potentially removes the bullet (the "this" object) from the world. However, your act() method calls checkEdge() and then always also calls checkKill(). The checkKill() method calls getOneObjectAtOffset(...), which is invalid if this object is no longer in the world, and at that point you get the exception.
b321234 b321234

2011/12/15

#
davmac, I somewhat get what you said I think.... So I need to do something like what danpost said - to make an if statement right? It's because I can't always call the checkKill() method if the butllet's already gone, so it'll always throw this error. Okay I get it... Thanks a lot!!! Thanks danpost!!
b321234 b321234

2011/12/15

#
Problem solved!!! YAyyyyyyyyy!!!! Thanks again!!
kiarocks kiarocks

2011/12/15

#
Another idea is to remove the object at the end of the act() method.
You need to login to post a reply.