-1

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);
    }
}
D McCracken
  • 33
  • 1
  • 8
  • Where & how is your cash register instantiated? If you are getting an NPE, it means that it is not. Also, do not use double for money, use big decimal. – jumping_monkey Sep 02 '20 at 01:41

1 Answers1

0

Good day, You are neither instantiating your class nor passing its instance to the target function.