I am having a weird issue where the function in my class keep returning NullPointerException, Here is my code below where I am referring to the cashRegister class and each time I use the functions (cashRegister.add() or cashRegister.toString()) it returns nullPointerException. Does anyone know how to fix?
private void sell() {
Product product = product(readName());
System.out.println("Selling " + product.getName());
int number = readNumber();
if (product.has(number)) {
double sale = product.sell(number);
cashRegister.add(sale);
}
else {
System.out.println("Not enough stock");
}
}
private void viewCash() {
System.out.println(cashRegister.toString());
}
import java.text.*;
public class CashRegister {
private double cash;
public CashRegister() {
}
public void add(double money) {
cash += money;
}
@Override
public String toString() {
return "Cash: $" + formatted(cash);
}
private String formatted(double amount) {
return new DecimalFormat("###,##0.00").format(amount);
}
}