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

2016/5/23

Help with a java.lang.NullPointerException

HamHam HamHam

2016/5/23

#
So i'm trying to make a clicker, and everything compiles fine, but whenever I click my button i get a java.lang.NullPointerException. Can anybody see what might be the problem? Btw I also copy pasted from projects I did a while back so that might be the problem. Button code -
import greenfoot.*;

/**
 * Write a description of class Button here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Button extends Actor
{
    private Counter scoreCounter;
    GreenfootSound music = new GreenfootSound("horn.wav");
    Space space = (Space) getWorld();
    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Click();
    }   
    public Button()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() / 2, image.getHeight() / 2);
        setImage(image);
    }
    public void Click()
    {
        if (Greenfoot.mouseClicked(this))
 
        {
            scoreCounter.add(1);
        }
    }
}
Counter code -
import greenfoot.*;
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    private Item item;
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        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.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }   
    
    public void pay(int amount)
    {
        if (amount >= target)
        {
            target -= amount;
            item.ifAllowed();
        }
    }
}
Thank you to anyone who helps in advance.
SPower SPower

2016/5/23

#
In the Button class you posted, you have an instance variable called scoreCounter, but you never initialise that variable, so it's null. Hence, when you call add(1) on it (line 33), you get an exception.
You need to login to post a reply.