-2

I am trying to create a program where a car dealership can pick what kind of customer and then enter in the info. It works fine up to where it prints the results. Then it gives me the error at the bottom. PLEASE HELP!! **I also need a way to add up the service objects and print them as well. Any help is appreciated! thanks in advance

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        final int MAX = 9999;
        Customer [] cust = new Customer[MAX];
        int choice = 0;
        int cnt;

        double total;

        for(cnt=0; cnt < MAX && choice == 1 || choice ==2 || choice == 0; cnt++){
            System.out.println("For a Service customer type 1, for a Purchaser type 2, to terminate the program press 9");
            choice = s.nextInt();
            switch (choice){
            case 1:
                cust [cnt] = new Service();
                break;
            case 2:
                cust [cnt] = new Purchaser();
                            break;
            default:
                break;
            }
        }
        for(int i=0; i < cnt; i++){
            cust[i].showData();
        }
        //for(int i=0; i < cnt; i++ ){
            //total = cust.
        //}
            s.close();

    }
}
interface Functions {
    public void getData();
    public void showData();
}
abstract class Customer implements Functions {
    protected String name;


}
class Purchaser extends Customer {
    protected double payment;

    public Purchaser(){
        getData();
    }

    public void getData() {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the name of the customer");
        name = s.nextLine();
        System.out.println("Enter payment amount: ");
        payment = s.nextDouble();
    }
    public void showData() {    
        System.out.printf("Customer name: %s Payment amount is: %.2f\n",name,payment);

    }   
}
class Service extends Customer {
    protected String date;
    protected double amount;
    public Service () {
        getData();

    }

    public void getData() {     
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the name of the customer");
        name = s.nextLine();
        System.out.println("Enter date of Service: ");
        date = s.nextLine();
        System.out.println("Enter the cost of Service: ");
        amount = s.nextDouble();
    }
    public void showData() {
        System.out.printf("Customer name: %s The date is: %s, the Amount owed is: %.2f\n",name, date, amount);
    }
}

Customer name: ;khhihl The date is: ljhljh, the Amount owed is: 555.00
Customer name: ljhjlhhj Payment amount is: 545454.00
Exception in thread "main" java.lang.NullPointerException
    at prog24178.assignment.Assignment3.main(Assignment3.java:30)
user278153
  • 75
  • 8

1 Answers1

0

I would suggest, inside the for loop with the call to showData(), that you add the following statement:

if(cust[i]!=null)

This will make sure that it never tries to read data from a space in the array where there is no customer. (Null pointer exceptions are caused by the program trying to access objects that have no data.)

You could get around most of the problem by using an ArrayList<Customer> instead of a Customer[]. This way, you can have any arbitrary number of customers, and if you get to the end of the customers that you have added, it doesn't try to look in places where there are no customers.

Epiglottal Axolotl
  • 1,040
  • 7
  • 17