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) {
}
}
}