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

2020/1/23

Why does this cause an IllegalStateException?

davkri22 davkri22

2020/1/23

#
public void act(){
        if (Greenfoot.mouseClicked(this)){
            try{
                Class cls = Class.forName(type);
                tower = (Tower)cls.newInstance();               
            } catch (Exception e){
                System.err.println(e);
            }
            getWorld().addObject(tower, 0, 0);
        }
        if (tower != null && !tower.placed && tower.getWorld() != null){
            buy(tower);
        }
    } 
This code creates an IllegalStateException with the message: 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 have very similar code in other classes but for some reason, this is not working. Does anyone know why?
davkri22 davkri22

2020/1/23

#
The error happens within the try loop
davkri22 davkri22

2020/1/23

#
Nevermind, the error was really coming from my tower constructor but greenfoot did not show me.
You need to login to post a reply.