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

2013/11/22

Null Pointer exception

Banginwithflavor Banginwithflavor

2013/11/22

#
I have a game where the player controls a spaceship. I want the player to be able to press a button to release small fighters. These fighters should wander around the ships area however when i try to grab the coordinates from the ship i get a null pointer exception. // in world
Ship ship = new Ship();  
addObject(ship,350,350);
// in ship
public void launchFighter()
    {
        Fighter fighter = new Fighter();
        if(Greenfoot.isKeyDown("Q"))
        {
            getWorld().addObject(fighter,getX(),getY());
        }
    }
//in Fighter Class
/**
     * returns distance from ship
     */
    public double getDistanceFromShip(Ship ship)
    {
        //Fix This
       return Math.sqrt(Math.pow(getX()-ship.getX(),2)+Math.pow(getY()-ship.getY(),2)); 
    }
i think this is all that is needed to correct this.
Gevater_Tod4711 Gevater_Tod4711

2013/11/22

#
If the problem is in line 7 of your Fighter class you probably call this method with the parameter null. So the problem is not in this method but wherever this method is called with the parameter null. To fix the exception you could just check whether ship is null before calculating the distance like this:
    /** 
         * returns distance from ship 
         */  
        public double getDistanceFromShip(Ship ship)  
        {  
           if (ship == null) {
               return -1;// or whatever you want to return then;
           }
            //Fix This  
           return Math.sqrt(Math.pow(getX()-ship.getX(),2)+Math.pow(getY()-ship.getY(),2));   
        }  
But this will only stop the exceptions. The method will then return -1 and this is only a default value so that might cause other problems. If you post the stack trace (the text in your console) we probably can help you fixing the origin of this problem and not only treat the symptoms.
danpost danpost

2013/11/22

#
You are not showing the method where 'getDistanceFromShip' is called (again, indicate the class it is in).
Banginwithflavor Banginwithflavor

2013/11/22

#
oops this is also in the fighter class
     /**
     * wander around capital ship
     */
    public void wander()
    { 
        if(getDistanceFromShip(ship) > 50.0)
        {
            turnTowards(ship.getX(),ship.getY());
        }
        move(3);       
    }
Gevater_Tod4711 Gevater_Tod4711

2013/11/22

#
The problam probably is that ship in line 6 sometimes is null. That causes the NullPointerException in your getDistanceFromShip method.
danpost danpost

2013/11/22

#
OK, where is code that calls the 'wander' method.
Banginwithflavor Banginwithflavor

2013/11/22

#
wander() is called in the act() method in fighter. Im confused as to why it would be null though Because the ship is always in the world.
Gevater_Tod4711 Gevater_Tod4711

2013/11/22

#
If you post the whole code we can help you fixing this.
danpost danpost

2013/11/22

#
If the Fighter class has a field that holds the Ship object that is in the world, then you should not be having any problems. However, I do not see where you may be passing the ship created and put into the world to any of the Fighter object. Please show the entire Fighter class code.
Banginwithflavor Banginwithflavor

2013/11/23

#
Here is the link to the scenario . and here is the rest of the fighter class.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Fighter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Fighter extends Mover
{
    private Ship ship;  
    /**
     * Act - do whatever the Fighter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        wander();
    }    
     /**
     * wander around capital ship
     */
    public void wander()
    { 
        if(getDistanceFromShip(ship) > 50.0)
        {
            turnTowards(ship.getX(),ship.getY());
        }
        move(3);       
    }
    /**
     * returns distance from ship
     */
    public double getDistanceFromShip(Ship ship)
    {
        //Fix This
       return Math.sqrt(Math.pow(getX()-ship.getX(),2)+Math.pow(getY()-ship.getY(),2)); 
    }
}
danpost danpost

2013/11/23

#
Line 11 declares an instance field that is to hold a Ship object called 'ship'. In and of itself, the value of 'ship' is 'null'. It has not been set to any Ship object as yet. I noticed that it is declared with 'private' access. This is not a problem; but, it does tell me that it cannot be set from another class (which is actually good programming). At any rate, nowhere in the class is 'ship' ever set to the Ship object that you have in your world and remains 'null; hence, your NullPointerExceptions. The best way to set the 'ship' field to the Ship object you have in your world is through the Fighter class constructor method; especially since it is your Ship object that creates the fighters.
// in Ship class, when creating a Fighter object
Fighter fighter = new Fighter(this); // 'this' refers to the Ship object
getWorld().addObject(fighter, /* coordinates */);
// in Fighter class, add the following constructor method
public Fighter(Ship baseShip) // receives Ship object
{
    ship = baseShip; // sets instance field for 'this' object
}
Banginwithflavor Banginwithflavor

2013/11/23

#
Excellent!!! Thanks for teaching me how to do that!
You need to login to post a reply.