I get this error every time I try to run my program:
java.lang.NullPointerException
at Cruiser.removeLives(Cruiser.java:106)
at Cruiser.isShot(Cruiser.java:93)
at Cruiser.act(Cruiser.java:37)
I cant figure out whats wrong
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Cruiser here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Cruiser extends Actor
{
/**
* Act - do whatever the Cruiser wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
//creates the integer variables gunHeat, lives, and timer
private int gunHeat;
private int lives;
private int timer;
public Cruiser()
{
//sets the gunHeat to start at 0, lives to start at 3, and timer to strat at 0
gunHeat = 0;
lives = 3;
timer = 0;
}
public void act()
{
//activates the methods
motionControl();
fire();
//subtracts one from gunheat 50 times a second
gunHeat--;
isShot();
//fuelLevel();
powerUp();
}
private void motionControl()
{
//controls the Cruiser with arrow keys
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY() - 3);
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY() + 3);
}
//if the cruiser reaches the top or bottom of the screen it will loop to the other end
if (getY() < 400)
{
setLocation(getX(), 400);
}
if (getY() > 600)
{
setLocation(getX(), 600);
}
}
private void fire()
{
//adds the missile if you press space but will only do it every 1/2 a second it also plays the sound effect of a laser shooting
if (Greenfoot.isKeyDown("space") && (gunHeat < 0))
{
getWorld().addObject(new Missile(), getX(), getY());
gunHeat = 25;
//plays the missile sound file
Greenfoot.playSound("shot1.wav");
}
}
public void isShot()
{
// checks if the laser and cruiser are intersecting
Actor laser = getOneIntersectingObject(Laser.class);
//if the is touching the cruiser it will remove the image, subtract one from lives, and activates the remove lives to display the new amount of lives on the scoreboard
if (laser != null)
{ getWorld().removeObject(laser);
lives --;
if (lives<0) {
Space space = (Space) getWorld();
space.gameOver();
}else{
removeLives();
}
}
}
public void removeLives()
{
//gets the class space
Space world = (Space) getWorld();
//gets the scoreboard class
Scoreboard scoreboard = world.getScoreboard();
//activates the updateScore method from the scoreboard class and update the new amount of lives on the scoreboard
scoreboard.updateScore(scoreboard.getPoints(), scoreboard.getPower(),"Lives: " + lives);
}
private void fuelLevel()
{
//adds one to the timer every time the method is run (50 times a second)
timer ++;
//if the timer is greater than or equal to 100 (every 2 seconds) it will...
if (timer >= 100)
{
//get the space class
Space world = (Space) getWorld();
//get the scoreboard class
Scoreboard scoreboard = world.getScoreboard();
//grab the update score method and subtract one from the health bar
scoreboard.updateScore(scoreboard.getPoints(), scoreboard.getPower() - 1, scoreboard.getLives());
//resets the timer
timer = 0;
if (scoreboard.power <=0)
{
Space space = (Space)getWorld();
space.gameOver();
}
}
}
public void powerUp(){
Actor powerpod = getOneIntersectingObject(PowerPod.class);
if (powerpod != null)
{
getWorld().removeObject(powerpod);
Space world = (Space) getWorld();
Scoreboard scoreboard = world.getScoreboard();
scoreboard.updateScore(scoreboard.getPoints(), 100, scoreboard.getLives());
}
}
}
