Everything works fine until i die and it shows me error. how do i fix this error;
" am i a using generic list<> correctly or not to get remaining Asteroid object in background.
code is between line 131-141 in rocket class
java.lang.NullPointerException at Rocket.leftObject(Rocket.java:136) at Rocket.act(Rocket.java:46) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
//Rocket class
import greenfoot.*;
import java.util.List;
/**
* A rocket that can be controlled by the arrowkeys: up, left, right.
* The gun is fired by hitting the 'space' key. 'z' releases a proton wave.
*
* @author Poul Henriksen
* @author Michael Kölling
*
* @version 1.0
*/
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;
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++;
leftObject();
}
/**
* 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;
}
}
private void leftObject(){
Space space =(Space)getWorld();
World world =getWorld();
List<Asteroid> left =world.getObjects(Asteroid.class);
if (left.isEmpty())
{
space.add();
}
}
}
import greenfoot.*;
/**
* Space. Something for rockets to fly in.
*
* @author Michael Kölling
* @version 1.2
*/
public class Space extends World
{
private Counter scoreCounter;
private int startAsteroids = 2;//Game will start with 2 asteroids
/**
* Create the space and all objects within it.
*/
public Space()
{
super(600, 500, 1);
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
background.fill();
createStars(300);
Rocket rocket = new Rocket();
addObject(rocket, getWidth()/2 + 100, getHeight()/2);
addAsteroids(startAsteroids);
scoreCounter = new Counter("Score: ");
addObject(scoreCounter, 60, 480);
Explosion.initializeImages();
ProtonWave.initializeImages();
}
/**
* Add a given number of asteroids to our world. Asteroids are only added into
* the left half of the world.
*/
private void addAsteroids(int count)
{
for(int i = 0; i < count; i++)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Asteroid(), x, y);
}
}
/**
* Crete a given number of stars in space.
*/
private void createStars(int number)
{
GreenfootImage background = getBackground();
for(int i=0; i < number; i++)
{
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
int color = 120 - Greenfoot.getRandomNumber(100);
background.setColor(new Color(color,color,color));
background.fillOval(x, y, 2, 2);
}
}
/**
* This method is called when the game is over to display the final score.
*/
public void gameOver()
{
addObject(new ScoreBoard(scoreCounter.getValue()), getWidth()/2, getHeight()/2);
}
/**
* This is for to update the score
*/
public void countScore(int change)
{
scoreCounter.add(change);
}
public void add()
{
startAsteroids++;
addAsteroids(startAsteroids);
}
}
