Hi, I'm getting this NullPointerException in one of my classes (state). This design is done by a state machine design pattern. From debugging it I can see that insertMoney under coffeemachine>State>insertMoney is null but I can't figure out why it's null.

The line with the error is almost on the bottom and marked with ERROR.
Error message:

Exception in thread "Thread-3" java.lang.NullPointerException
        at hotdrinksmachine.MessageDelay.run(InsertMoney.java:102)
        at java.lang.Thread.run(Thread.java:619)

This is my first semester in Java so please help! :)

I attached my project from NetBeans and also just the .java files.

package hotdrinksmachine;

import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * Handles the payment for the selected drink
 * @author Tómas
 */
public class InsertMoney extends State {
    private InsertMoney insertMoney;

    /**
     * Empty constructor that initializes the superclass variables 
     */
    public InsertMoney() {
    }

    /**
     * Constructor to initializes the State variables
     * @param state previous state
     */
    public InsertMoney(State state) {
        super(state.getDrinkPrice(), state.getTotalInsertedMoney(), state.getSelection());
    }

    public void makeSelection(String selection, CoffeeMachine coffeemachine) {
    }

    public void setPrice(int drinkPrice, CoffeeMachine coffeemachine) {
    }

    /**
     * Decides if the inserted money is too low, too much or sufficient to
     * change state
     * @param money keeps track of total amount of money put in the
     * CoffeeMachine
     * @param coffeemachine reference to CoffeeMachine.java
     */
    @Override
    public void insertMoney(int money, CoffeeMachine coffeemachine) {
        int totalMoneyInserted = money + super.getTotalInsertedMoney();
        super.setTotalInsertedMoney(totalMoneyInserted);
        if (super.getDrinkPrice() > totalMoneyInserted) { // Less than total
            coffeemachine.displayNotEnoughMoney(super.getDrinkPrice() - totalMoneyInserted);
        } else if (super.getDrinkPrice() < totalMoneyInserted) { // Greater than total
            coffeemachine.displayReturnMoney(totalMoneyInserted - super.getDrinkPrice());
            MessageDelay messageDelay = new MessageDelay(coffeemachine, insertMoney);
            Thread delay = new Thread(messageDelay);
            delay.start();

            //super.setTotalInsertedMoney(super.getDrinkPrice()); // Set totalInsertedMoney to total price of drink
            //coffeemachine.setState(new BrewDrink(this));
            //coffeemachine.getState().brewDrink(coffeemachine);
        } else { // Equals total drink price
            coffeemachine.setState(new BrewDrink(this));
            coffeemachine.getState().brewDrink(coffeemachine);
        }
    }

    @Override
    public void brewDrink(CoffeeMachine coffeemachine) {
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public void setDrinkReady(boolean drinkReady, CoffeeMachine coffeemachine) {
    }

    @Override
    public void drinkRemoved(boolean drinkReady, CoffeeMachine coffeemachine) {
    }
}
class MessageDelay implements Runnable {

    private CoffeeMachine coffeemachine;
    private InsertMoney insertMoney;
    private State state;

    public MessageDelay() {
    }

    public MessageDelay(CoffeeMachine coffeemachine, InsertMoney insertMoney) {
        this.coffeemachine = coffeemachine;
        this.insertMoney = insertMoney;
    }

    public void run() {
        try {
            //coffeemachine.displayReturnMoney(10);
            Thread.sleep(1000);
ERROR-->state.setTotalInsertedMoney(state.getDrinkPrice()); // Set totalInsertedMoney to total price of drink
            coffeemachine.setState(new BrewDrink(insertMoney));
            coffeemachine.getState().brewDrink(coffeemachine);
        } catch (InterruptedException ex) {
        }
    }
}

Recommended Answers

All 2 Replies

class MessageDelay implements Runnable {

    private CoffeeMachine coffeemachine;
    private InsertMoney insertMoney;
    private State state;

    public MessageDelay() {
    }

    public MessageDelay(CoffeeMachine coffeemachine, InsertMoney insertMoney) {
        this.coffeemachine = coffeemachine;
        this.insertMoney = insertMoney;
    }

    public void run() {
        try {
            //coffeemachine.displayReturnMoney(10);
            Thread.sleep(1000);
ERROR-->state.setTotalInsertedMoney(state.getDrinkPrice()); // Set totalInsertedMoney to total price of drink
            coffeemachine.setState(new BrewDrink(insertMoney));
            coffeemachine.getState().brewDrink(coffeemachine);
        } catch (InterruptedException ex) {
        }
    }
}

Your problem exists in this code segment. When your thread is being run it tries to set the total money inserted by the state. However at this point state is null. In you constructor (or by another method) you need to initialize state. Hope that helps.

Cheers

Check line #88 of your code. the state varaible is only declared, not initialized. By default instatnce varaibles are null, and you use this state object on line # 102 which gives you the NullPointException.

Always initialize variables before use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.