I am working on this asteroid game inside of greenfoot and my code keeps spiting out a
java.lang.NullPointerException.
The code is below. Error occurs on line 50:
count = world.numberOfObjects();.
I know there are several posts about this error but the research I've done about this error shows it occurs when you try to use a reference that points to null. However, I have initialized my variable so i'm not sure why this is happening. I'm still fairly new to programming so I'm probably just not understanding this correctly. Any help would be great. Thanks!
public class Rocket extends SmoothMover
{
private static final int gunReloadTime = 5; // The minimum delay between firing the gun.
private static final int protonReloadTime = 500; // The minimum delay between proton wave bursts.
private int reloadDelayCount; // How long ago we fired the gun the last time.
private int protonDelayCount; // How long ago we fired the proton wave the last time.
private int count = 0; //count the number of asteroids
private GreenfootImage rocket = new GreenfootImage("rocket.png");
private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");
/**
* Initialise this rocket.
*/
public Rocket()
{
reloadDelayCount = 5;
protonDelayCount = 500;
addToVelocity(new Vector(13, 0.7)); // initially slowly drifting
}
/**
* Do what a rocket's gotta do. (Which is: mostly flying about, and turning,
* accelerating and shooting when the right keys are pressed.)
*/
public void act()
{
move();
checkKeys();
checkCollision();
reloadDelayCount++;
protonDelayCount++;
Space space = (Space)getWorld(); //call space objects
World world = getWorld(); //call world objects
count = world.numberOfObjects(); //ERROR IS HERE
/**
* Add an asteroid when the starting ones are cleared
*/
if(count <=2)
{
space.add();
}
}
/**
* Check whether there are any key pressed and react to them.
*/
private void checkKeys()
{
ignite(Greenfoot.isKeyDown("up"));
if (Greenfoot.isKeyDown("left"))
{
turn(-5);
}
if (Greenfoot.isKeyDown("right"))
{
turn(5);
}
if (Greenfoot.isKeyDown("space"))
{
fire();
}
if (Greenfoot.isKeyDown("z"))
{
startProtonWave();
}
}
/**
* Check whether we are colliding with an asteroid.
*/
private void checkCollision()
{
Asteroid a = (Asteroid) getOneIntersectingObject(Asteroid.class);
if (a != null)
{
Space space = (Space) getWorld();
space.addObject(new Explosion(), getX(), getY());
space.removeObject(this);
space.gameOver();
}
}
/**
* Should the rocket be ignited?
*/
private void ignite(boolean boosterOn)
{
if (boosterOn)
{
setImage(rocketWithThrust);
addToVelocity(new Vector(getRotation(), 0.3));
}
else
{
setImage(rocket);
}
}
/**
* Fire a bullet if the gun is ready.
*/
private void fire()
{
if (reloadDelayCount >= gunReloadTime)
{
Bullet bullet = new Bullet (getVelocity(), getRotation());
getWorld().addObject (bullet, getX(), getY());
bullet.move ();
reloadDelayCount = 0;
}
}
/**
* Release a proton wave (if it is loaded).
*/
private void startProtonWave()
{
if (protonDelayCount >= protonReloadTime)
{
ProtonWave wave = new ProtonWave();
getWorld().addObject (wave, getX(), getY());
protonDelayCount = 0;
}
}
}
This is a simple null
check on that object.
Space space = (Space)getWorld(); //call space objects
World world = getWorld(); //call world objects
if(world != null){
count = world.numberOfObjects();
}else{
//handle exception
}