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

2013/12/17

Null Pointer Exception

JasonZhu JasonZhu

2013/12/17

#
I am recieving a null pointer exception. java.lang.NullPointerException at SmoothMover.move(SmoothMover.java:43) at Supply.act(Supply.java:35) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
    /**
     * Act - do whatever the Supply wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkPowerUp();     
        move();        
        life--;
        if(life==0){
            getWorld().removeObject(this);
            life=1000;
        }        
    }    
    /**
     * Move in the current movement direction. Wrap around to the opposite edge of the
     * screen if moving out of the world.
     */
    public void move() 
    {
        exactX = exactX + movement.getX();
        exactY = exactY + movement.getY();
        if(exactX >= getWorld().getWidth()) {
            exactX = 0;
        }
        if(exactX < 0) {
            exactX = getWorld().getWidth() - 1;
        }
        if(exactY >= getWorld().getHeight()) {
            exactY = 0;
        }
        if(exactY < 0) {
            exactY = getWorld().getHeight() - 1;
        }
        super.setLocation((int) exactX, (int) exactY);
    }
JasonZhu JasonZhu

2013/12/17

#
One of my friends said it was the my World returning null, is this the case and how do I fix it? The error happens when I touch the Supply. This is the entire code of the classes:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Supply here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Supply extends SmoothMover
{
    private Vector singlePush = new Vector(Greenfoot.getRandomNumber(360),1);
    private int life;
    private int type;
    
    public Supply(int type)
    {
        addForce(singlePush);
        life = 350;
        switch(type){
            case 0: 
            setImage("gas.png");break;
            case 1:
            setImage("bullets.png");break;
        }
        this.type=type;
    }
    
    /**
     * Act - do whatever the Supply wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkPowerUp();     
        move();        
        life--;
        if(life==0){
            getWorld().removeObject(this);
            life=1000;
        }        
    }    
    
    private void checkPowerUp()
    {        
        Rocket intersectingRocket = (Rocket)getOneIntersectingObject(Rocket.class);
        if(intersectingRocket!=null){
            if(type==1){
                intersectingRocket.bullets+=50;
            }
            if(type==0){
                intersectingRocket.fuel+=100;
            }
            getWorld().removeObject(this);
        }                
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A variation of an actor that maintains precise location (using doubles for the co-ordinates
 * instead of ints). It also maintains a current movement in form of a movement vector.
 * 
 * This is a variation of the SmoothMover class presented ealier in the book (version 2.0).
 * This version implements wrap-around movement: when the actor moves out of the world at one
 * side, it enters it again at the opposite edge.
 * 
 * @author Poul Henriksen
 * @author Michael Kolling
 * 
 * @version 2.1
 */
public abstract class SmoothMover extends Actor
{
    private Vector movement;
    private double exactX;
    private double exactY;
    
    public SmoothMover()
    {
        this(new Vector());
    }
    
    /**
     * Create new thing initialised with given speed.
     */
    public SmoothMover(Vector movement)
    {
        this.movement = movement;
    }
    
    /**
     * Move in the current movement direction. Wrap around to the opposite edge of the
     * screen if moving out of the world.
     */
    public void move() 
    {
        exactX = exactX + movement.getX();
        exactY = exactY + movement.getY();
        if(exactX >= getWorld().getWidth()) {
            exactX = 0;
        }
        if(exactX < 0) {
            exactX = getWorld().getWidth() - 1;
        }
        if(exactY >= getWorld().getHeight()) {
            exactY = 0;
        }
        if(exactY < 0) {
            exactY = getWorld().getHeight() - 1;
        }
        super.setLocation((int) exactX, (int) exactY);
    }
    
    /**
     * Set the location from exact coordinates.
     */
    public void setLocation(double x, double y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation((int) x, (int) y);
    }
    
    /**
     * Set the location from int coordinates.
     */
    public void setLocation(int x, int y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation(x, y);
    }

    /**
     * Return the exact x-coordinate (as a double).
     */
    public double getExactX() 
    {
        return exactX;
    }

    /**
     * Return the exact y-coordinate (as a double).
     */
    public double getExactY() 
    {
        return exactY;
    }

    /**
     * Increase the speed with the given vector.
     */
    public void addForce(Vector force) 
    {
        movement.add(force);
    }
    
    /**
     * Accelerate the speed of this mover by the given factor. (Factors < 1 will
     * decelerate.)
     */
    public void accelerate(double factor)
    {
        movement.scale(factor);
        if (movement.getLength() < 0.15) {
            movement.setNeutral();
        }
    }
    
    /**
     * Return the speed of this actor.
     */
    public double getSpeed()
    {
        return movement.getLength();
    }
    
    /**
     * Stop movement of this actor.
     */
    public void stop()
    {
        movement.setNeutral();
    }
    
    /**
     * Return the current speed.
     */
    public Vector getMovement() 
    {
        return movement;
    }
}
danpost danpost

2013/12/17

#
It would be better if your Rocket object check for the Supply objects instead of the way you have it here. That way, you can adjust the fields in that class directly and remove the Supply object without causing an error. You will need a 'get' method in the Supply class to return its value of 'type'. Line 39 seems to be meaningless, as once the object is removed from the world due to life time expended, no references remain on it (it cannot be further accessed).
davmac davmac

2013/12/17

#
You are getting the exception because in the checkPowerUp method you (may) remove 'this' object from the world. However, after calling checkPowerUp you call move(), which calls getWorld().getWidth(). If an object has been removed from the world, then calling its getWorld() method will return null. The best bet is to avoid calling move() if the object has already been removed from the world. See this explanation.
JasonZhu JasonZhu

2013/12/17

#
Thanks guys, I fixed the problem
You need to login to post a reply.