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

2020/8/29

NullPointerException with getObjects()

nebuto nebuto

2020/8/29

#
Hello, I have a simple World with two Actors, Auto and Zielsichrakete(Homing Missile) I try to make a Homing Missile, that needs the coordinates from a Auto Object. When trying to get the Reference i am getting a NullPointerException in class Zielsuchrakete. Class Zielsuchrakete
public class Zielsuchrakete extends Actor
{
    private Auto target;
    private int deltaX;
    private int deltaY;
    private int geschwindigkeit = 5;

    public Zielsuchrakete() {
        this. target = this.getWorld().getObjects(Auto.class).get(0);
    }

    /**
     * Act - do whatever the Zielsuchrakete wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // move(-1);
        // if(isAtEdge()) turn(180);// this.setLocation(this.getX() - this.deltaX*this.geschwindigkeit/(this.deltaX*this.deltaX), this.getY()+this.deltaY * this.geschwindigkeit/(this.deltaX + this.deltaY));
        // // Add your action code here.
    }    
}
Class MyWorld
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        this.addObject(new Auto(), 0, getHeight()/2);
        this.addObject(new Zielsuchrakete(), getWidth(), getHeight()/2);
        
    }
}
Can someone help me out? Best wishes and a great day, Sebastian
danpost danpost

2020/8/29

#
nebuto wrote...
I have a simple World with two Actors, Auto and Zielsichrakete(Homing Missile) I try to make a Homing Missile, that needs the coordinates from a Auto Object. When trying to get the Reference i am getting a NullPointerException in class Zielsuchrakete. << Code Omitted >>
The getWorld method will return any world the actor may be in. However, it returns a null value if not in a world; and no actor is in a world during construction. Therefore, using getWorld in a constructor is pointless. As an alternative, you can override the addedToWorld method or do one of several other things.
nebuto nebuto

2020/8/30

#
Thank You very much. That was my problem. I created a init()-method in Zielsuchrakete, that runs once in the act()-method.
private boolean initialisierung = true;

    /**
     * Act - do whatever the Zielsuchrakete wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (this.initialisierung) {
            init();
            initialisierung = false;

        }
}    

    public void init() {
        this.target = this.getWorld().getObjects(Auto.class).get(0);
        

    }
danpost danpost

2020/8/30

#
nebuto wrote...
Thank You very much. That was my problem. I created a init()-method in Zielsuchrakete, that runs once in the act()-method. << Code Omitted >>
That is fine. However, the addedToWorld method is already called just once when the actor is placed into a world. So, without line 1 and without lines 9 thru 13, you can replace the init method with:
protected void addedToWorld(World world) {
    this.target = world.getObjects(Auto.class).get(0);
}
nebuto nebuto

2020/8/30

#
Perfect! Got it. Thanks a lot. And after trying to write a homing calculation for my Missile with a lot of Analysis/Trigonometrie, i finally found the turnTowards-Method :-)
You need to login to post a reply.