为什么我收到错误!?(空指针异常) [英] Why am I getting an error!?(Null pointer Exception)

查看:61
本文介绍了为什么我收到错误!?(空指针异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,让汽车经销商可以选择什么样的客户,然后输入信息.它可以正常工作到打印结果的位置.然后它给了我底部的错误.请帮忙!!**我还需要一种方法来添加服务对象并打印它们.任何帮助表示赞赏!提前致谢

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)

推荐答案

我建议,在调用 showData() 的 for 循环内,添加以下语句:

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.)

您可以通过使用 ArrayList 而不是 Customer[] 来解决大部分问题.通过这种方式,您可以拥有任意数量的客户,并且如果您添加到已添加客户的末尾,它就不会尝试查找没有客户的地方.

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.

这篇关于为什么我收到错误!?(空指针异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆