Hello, I'm new to Java and I need a bit of help with an error I'm getting. When I try to use the 'add' method in the 'Score' class I get a NullPointerException error.
The code that throws the error:
The code that contains the 'add' method:
The code for my 'player' class that uses elements from the 'Score' class:
If anyone could help to 'point' me in the right direction, it would be a great help!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Enemy1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Enemy1 extends Enemies
{
/**
* Act - do whatever the Enemy1 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Score score;
public void act()
{
setRotation(180);
if( canSee(PlayerProjectile.class) ){
kill(PlayerProjectile.class);
//Greenfoot.playSound("slurp.wav");
score.add(25);
getWorld().removeObject(this);
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* A simple counter with graphical representation as an actor on screen.
*
* @author mik
* @version 1.0
*/
public class Score extends Actor{
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
public int value;
public int target;
/**
* Create a new counter, initialised to 0.
*/
public Score(){
background = getImage(); // get image from class
value = 0;
target = 0;
updateImage();
}
/**
* Animate the display to count up (or down) to the current target value.
*/
public void act(){
if (value < target) {
value++;
updateImage();
}
else if (value > target) {
value--;
updateImage();
}
}
/**
* Add a new score to the current counter value.
*/
public void add(int score){
target += score;
}
/**
* Return the current counter value.
*/
public int getValue(){
return value;
}
/**
* Set a new counter value.
*/
public void setValue(int newValue){
target = newValue;
value = newValue;
updateImage();
}
/**
* Update the image on screen to show the current value.
*/
private void updateImage(){
GreenfootImage image = new GreenfootImage(background);
GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
setImage(image);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends Characters{
// Declares variables for the animation of the Player character.
private GreenfootImage playerImage1 = new GreenfootImage("Player 1.png");
private GreenfootImage playerImage2 = new GreenfootImage("Player 2.png");
private static final int bulletTimer = 5;
private int bulletCount = Integer.MAX_VALUE;
private Score score;
private Timer timer;
public static int finalscore = 0;
public Player (Score pointcounter, Timer pointTimer){
score = pointcounter;
timer = pointTimer;
}
private void animation(){
if( getImage() == playerImage1 ){
setImage( playerImage2 );
}
else {
setImage( playerImage1 );
}
}
public void act(){ // Run code here.
animation();
if( Greenfoot.isKeyDown("left") ){
move(-5);
}
if( Greenfoot.isKeyDown("right") ){
move(5);
}
if( Greenfoot.isKeyDown("space") ){
if(bulletCount > bulletTimer){
PlayerProjectile bullet = new PlayerProjectile();
getWorld().addObject(bullet, getX(),getY());
bulletCount = 0;
}
bulletCount++;
}
}
}
