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

2014/1/16

NullPointerException can’t find problem

Victor0409 Victor0409

2014/1/16

#
My code of my world is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MainWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainWorld extends World
{

    /**
     * Constructor for objects of class MainWorld.
     * 
     */
    public MainWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        Heading heading = new Heading();
        TankTurret turret = new TankTurret(heading);
        TankBase tank = new TankBase();
        addObject(tank, 200, 200);
        addObject(turret, 198, 199);
        turret.createHeading();
    }
}
The code of my TankTurret is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class TankTurret here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TankTurret extends Actor
{
    private Heading heading;

    public TankTurret()
    {
    }

    public TankTurret(Heading head)
    {
        heading = head;
    }

    /**
     * Act - do whatever the TankTurret wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        heading.turnTurret(getRotation());
    }
    
    public void turnNow(int degr)
    {
        turn(degr);
    }
    
    public void createHeading()
    {
        TankTurret turret = new TankTurret(heading);
        Heading heading = new Heading(turret);
        getWorld().addObject(heading, getX(), getY());
    }
}
And my heading code is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Heading here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Heading extends Actor
{
    private TankTurret turret;
    int rotatie;
    int rotatieSpeedRechts = 2;
    int rotatieSpeedLinks = -2;
    
    public Heading()
    {     
    }
   
    public Heading(TankTurret turr)
    {
        turret = turr;
        if (Greenfoot.getMouseInfo()== null)
        {

        }
        else
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            turnTowards((int)mouse.getX(), (int)mouse.getY());
        }
    }

    /**
     * Act - do whatever the Heading wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        turn();
    }    

    public void turn()
    {
        if (Greenfoot.getMouseInfo()== null)
        {

        }
        else
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            turnTowards((int)mouse.getX(), (int)mouse.getY());
        }
    }

    public void setRotationSpeed(int rotaSpeedLinks, int rotaSpeedRechts)
    {
        int rotatieSpeedRechts = rotaSpeedRechts;
        int rotatieSpeedLinks = rotaSpeedLinks;
    }

    public void turnTurret(int rotatie)
    {
        //turret.setHeading(getRotation());
        if(getRotation() > rotatie && getRotation() < rotatie + 180)
        {
            turret.turnNow(rotatieSpeedRechts);
        }
        else if(getRotation() > rotatie && getRotation() + 180 < rotatie || getRotation() - 180 < rotatie)
        {
            turret.turnNow(rotatieSpeedRechts);
        }
        else if(getRotation() < rotatie && getRotation() + 180 > rotatie)
        {
            turret.turnNow(rotatieSpeedLinks);
        }
        else if(getRotation() < rotatie && getRotation() + 180 > rotatie || getRotation() - 180 > rotatie)
        {
            turret.turnNow(rotatieSpeedLinks);
        }
    }
}
My error is in the Heading object in line 71
bourne bourne

2014/1/16

#
The problem lies with the fact that turret is null because using the constructor: public Heading() turret is not initialized like it is in its other constructor. The root of problem is in your MainWorld constructor where you call this problematic constructor.
davmac davmac

2014/1/16

#
Looks like turret is null, and you're trying to call a method on it. from MainWorld:
        Heading heading = new Heading();
This creates a Heading object with a null turret, and it looks like you never set turret anywhere.
davmac davmac

2014/1/16

#
Also, this looks wrong:
    public void createHeading()  
    {  
        TankTurret turret = new TankTurret(heading);  
        Heading heading = new Heading(turret);  
        getWorld().addObject(heading, getX(), getY());  
    }  
Why are you creating a new TankTurret? Shouldn't it just be:
    public void createHeading()  
    {  
        Heading heading = new Heading(this);  
        getWorld().addObject(heading, getX(), getY());  
    }  
You need to login to post a reply.