如何修复此空指针异常?

时间:2013-11-13 23:42:18

标签: java nullpointerexception

我不断获得nullPointerException,其余代码将无法运行。我有两个班级设置。一个是SalesPerson,另一个是main方法。标记了第42行,这是nullPointerException出现的位置。

这是第42行

System.out.printf("%-20s %15.2f %14.2f %n", list[i].getFirstName()  + " " + list[i].getLastName(), list[i].getTotalSales(), list[i].getSalary());





public class SalesPerson {
    private String firstName;
    private String lastName;
    private double firstSales;
    private double secondSales;
    private double thirdSales;
    private double fourthSales;
    private double totalSales;
    private double salary;

    public SalesPerson(String lastName, String firstName, double firstSales,
            double secondSales, double thirdSales, double fourthSales,
            double totalSales, double salary) {

        this.lastName = lastName;
        this.firstName = firstName;
        this.firstSales = firstSales;
        this.secondSales = secondSales;
        this.thirdSales = thirdSales;
        this.fourthSales = fourthSales;
        this.totalSales = totalSales;
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public double getTotalSales() {
        return totalSales;
    }

    public void setTotalSales(double totalSales) {
        this.totalSales = totalSales;
    }

    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 getFirstSales() {
        return firstSales;
    }

    public void setFirstSales(double firstSales) {

        if (firstSales >= 0) {
            this.firstSales = firstSales;
        } else {
            System.out.println("First sales must be greater than zero.");
        }
    }

    public double getSecondSales() {
        return secondSales;
    }

    public void setSecondSales(double secondSales) {
        if (secondSales >= 0) {
            this.secondSales = secondSales;
        } else {
            System.out.println("Second sales must be greater than zero.");
        }
    }

    public double getThirdSales() {
        return thirdSales;
    }

    public void setThirdSales(double thirdSales) {
        if (thirdSales >= 0) {
            this.thirdSales = thirdSales;
        } else {
            System.out.println("Third sales must be greater than zero.");
        }
    }

    public double getFourthSales() {
        return fourthSales;
    }

    public void setFourthSales(double fourthSales) {
        if (fourthSales >= 0) {
            this.fourthSales = fourthSales;
        } else {
            System.out.println("Fourth sales must be greater than zero.");
        }
    }

}

第二课

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

    public class Assignment10 {
        public static final int NUM_SALESPEOPLE = 20;
        public static final double NUM_PER_SALARY = 25;
        public static void main(String[] args) {

            SalesPerson[] list = new SalesPerson[NUM_SALESPEOPLE];

            try {
                int people = 0;
                Scanner fileInput = new Scanner(new File("A10.txt"));

                while (fileInput.hasNext()) {
                    String lastName = fileInput.next();
                    String firstName = fileInput.next();
                    double firstSales = fileInput.nextDouble();
                    double secondSales = fileInput.nextDouble();
                    double thirdSales = fileInput.nextDouble();
                    double fourthSales = fileInput.nextDouble();
                    double totalSales = firstSales + secondSales + thirdSales + fourthSales;
                    double salary = NUM_PER_SALARY * totalSales;
                    SalesPerson person = new SalesPerson(lastName, firstName,
                            firstSales, secondSales, thirdSales, fourthSales, totalSales, salary);
                    list[people] = person;
                    people++;

                }
            } catch (Exception ex) {
                System.out.println("Error opening file.");
            }

            System.out.println("Full Name                Total Sales         Salary");
            System.out.println("=========                ===========         ======");
            for (int i = 0; i < NUM_SALESPEOPLE; i++) {
                System.out.printf("%-20s %15.2f %14.2f %n",      list[i].getFirstName()  + " " + list[i].getLastName(), list[i].getTotalSales(), list[i].getSalary());
            }
            System.out.println("===================================================");


        }

    }

3 个答案:

答案 0 :(得分:5)

您正在创建一个大小为20的数组。此数组仅包含对NULL的引用。如果您的文件中包含的条目少于20个,则最终会产生nullpointerexception。

可以肯定的是,尝试打印人的价值。如果小于20则这是正常的。

答案 1 :(得分:1)

您的文件'A10.txt'没有20行(正确)。

您不应该创建数组。请改用ArrayListIterator来解决问题。

答案 2 :(得分:0)

只需添加一个null检查:

for (int i = 0; i < NUM_SALESPEOPLE; i++) {
    if (list[i] != null) {
        System.out.printf("%-20s %15.2f %14.2f %n",      list[i].getFirstName()  + " " + list[i].getLastName(), list[i].getTotalSales(), list[i].getSalary());
    }
}

或声明

int people = 0;

之前的两行(在try-block之前)并使用此数量的实际读取人数作为for循环的上限,而不是NUM_SALESPEOPLE