0

我不断得到一个nullPointerException,我的其余代码将无法运行。我有两个课程设置。一个是 the 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("===================================================");


        }

    }
4

3 回答 3

5

您正在创建一个大小为 20 的数组。该数组将仅包含对 NULL 的引用。如果在您的文件中您的条目少于 20 个,您最终会得到一个空指针异常。

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

于 2013-11-13T23:46:02.353 回答
1

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

你不应该创建一个数组。使用ArrayListandIterator代替,你不会有这样的问题。

于 2013-11-13T23:48:55.837 回答
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;

前两行(在您的尝试块之前)并使用此实际阅读人数作为 for 循环中的上限,而不是NUM_SALESPEOPLE.

于 2013-11-13T23:50:10.903 回答