初始化新对象时出现NullPointerException

时间:2014-11-17 04:42:56

标签: java nullpointerexception

我有一个名为CustomerRecord的类,另一个类CustomerList包含。当客户列表初始化时,一切都很好,但是当客户记录的第一个实例初始化时,我得到一个空指针异常。我不知道为什么会一直这样,但我非常感谢有关错误以及如何解决问题的一些帮助。

Exception in thread "main" java.lang.NullPointerException
at CustomerList.getCustomerList(CustomerList.java:31)
at Assignment3.main(Assignment3.java:16)

这是我的代码

public class CustomerRecord {

    private int customerNumber;
    private String firstName;
    private String lastName;
    private double balance;



    public CustomerRecord() {
        super();
    }
    public int getCustomerNumber() {
        return customerNumber;
    }
    public void setCustomerNumber(int customerNumber) {
        this.customerNumber = customerNumber;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public String toString(){
        return this.customerNumber  + " " + this.firstName  + " " + this.lastName  + " " + this.balance;

    }


}

这是我的CustomerList代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CustomerList {

private int count;
private CustomerRecord[] data;

public CustomerList(){
    count = 0;
    CustomerRecord[] data = new CustomerRecord[100];
    }

public void getCustomerList (String fileName){
    Scanner fileScan;

    try {
        fileScan = new Scanner(new File(fileName));

        while (fileScan.hasNext()){
            if (fileScan.hasNextInt()){
                int customerNumber = fileScan.nextInt();
                String firstName = fileScan.next();
                String lastName = fileScan.next();
                double TransactionAmount = fileScan.nextDouble();

                data[customerNumber].setBalance(data[customerNumber].getBalance() + TransactionAmount);
            }
            else{
                data[count] = new CustomerRecord();
                data[count].setCustomerNumber(count);
                data[count].setFirstName(fileScan.next());
                data[count].setLastName(fileScan.next());
                data[count].setBalance(fileScan.nextDouble());
                count++;
                }           
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

public CustomerRecord getCustomer (int customerNumber){
    if (data[customerNumber] != null){
        return data[customerNumber];
    }
    else
        return null;

}

}

1 个答案:

答案 0 :(得分:0)

当您在构造函数中初始化数据时,您实际上并没有引用相同的数据'你定义了。您创建了一个新数据并将其设置为100个位置。但是你在构造函数中定义了它,所以它并没有影响你的全局变量。您需要做的是替换CustomerRecord[] data = new CustomerRecord[100];

data = new CustomerRecord[100];