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)