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

2019/4/14

java.lang.IllegalStateException

Proprogrammer04 Proprogrammer04

2019/4/14

#
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. Hello evryone, my code threw an
java.lang.IllegalStateException
when i used the addObject tag. This is the line where the error happens:
getWorld().addObject(new factorydefinition("button","waste","categorys"),getX()-114,getY());
I don't understand this Problem, because the Actor will just be placed in the world...
nccb nccb

2019/4/14

#
I suspect the problem is to do with the calls to getX() and getY() near the end of the line. If the actor where that code resides is not itself in the world, these methods will throw an exception -- you can't have an X/Y position in the world if you're not in one. (In fact, getWorld() will return null too, so this exception would occur just ahead of a NullPointerException because of that.)
Proprogrammer04 Proprogrammer04

2019/4/14

#
The Actor should be in the World, because when I take the line where the error happens out, it works well...
danpost danpost

2019/4/14

#
Proprogrammer04 wrote...
The Actor should be in the World, because when I take the line where the error happens out, it works well...
Show the entire class code (using code tags) and point to the line in question.
Proprogrammer04 Proprogrammer04

4 days ago

#
Ok,
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class 
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class factorydefinition extends Factory
{
    String rule=" ";
    String myText=" ";
    String situation=" ";
    int TextX,TextY=0;
    int myX,myY=0;
    public factorydefinition(String givenRule,String text)
    {
        rule=givenRule;
        setText(text);
    }

    public factorydefinition(String givenRule,String text,String givenSituation)
    {
        rule=givenRule;
        //setText(text);
        situation=givenSituation;
    }

    public void act() 
    {
        myX=getX();
        myY=getY();
        getWorld().showText(""+situation,200,100);
        if(situation=="remove")
        {
            situationRemove();
        }
        if(situation=="categorys")
        {
            if(myText!="waste"||myText!="energy"||myText!="recources")
            {
                situationRemove();
            }
        }
        if(situation=="waste")
        {
            if(myText!="smallWI"||myText!="bigWI"||myText!="recycling")
            {
                situationRemove();
            }
        }
        if(situation=="energy")
        {
            if(myText!="coalPW"||myText!="nuclearPW"||myText!="hydroelectricPW"||myText!="solarPW"||myText!="windPW")
            {
                situationRemove();
            }
        }
        if(situation=="recources")
        {
            if(myText!="processingFactory"||myText!="sawmill"||myText!="wastewaterPW"||myText!="coalmine")
            {
                situationRemove();
            }
        }

        if(rule=="window")
        {
            window();
        }

        if(rule=="")
        {

        }
    }    

    public void window()
    {
        GreenfootImage image = new  GreenfootImage("FactoryDefinition.png");
        setImage(image);
        if(situation=="categorys")
        {
            situationCategorys();
        }
    }

    public void situationRemove()
    {
        getWorld().showText(" ",TextX,TextY);
        getWorld().removeObject(this);
    }

    public void situationCategorys()
    {
        System.out.println("situationCategory:1");
        getWorld().addObject(new factorydefinition("button","waste","categorys"),myX-114,myY);
        getWorld().addObject(new factorydefinition("button","recources","categorys"),myX,myY);
        getWorld().addObject(new factorydefinition("button","energy","categorys"),myX+114,myY);
        System.out.println("situationCategory:2");
    }
}
Proprogrammer04 Proprogrammer04

4 days ago

#
The line where the Problem happens was line 97. I made a few changes in the Code (getX() and getY() is now in act() line 31, 32) The problem for which I started the discussion is solved, but now it threw an NullPointerException. Just as nccb ecpected
nccb wrote...
(In fact, getWorld() will return null too, so this exception would occur just ahead of a NullPointerException because of that.)
I think the main problem is, that all these things (getWorld()/getX(),getY()) take place just after the object which executes the lines is added to the world. Perhaps, it al happens in the same tick. Could that be? If yes, how can I Change this?
danpost danpost

4 days ago

#
Add (insert) the following at line 66:
if (getWorld() == null) return;
See if that helps.
nccb nccb

4 days ago

#
If you want to be able to remove yourself from the world and add the new objects in same act() execution, one simple solution would be to execute the adding code before the removal code within act. That is, move the if (rule == "window") bit to the top of act, before you run the checking code to remove yourself from the world.
You need to login to post a reply.