0

を取得し続けnullPointerException、残りのコードは実行されません。私は2つのクラスをセットアップしています。1 つは で、もう 1 つはメソッド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 未満の場合、nullpointerexception が発生します。

確かに、人々の価値を印刷してみてください。20 未満の場合、これは正常です。

于 2013-11-13T23:46:02.353 に答える
1

あなたのファイル 'A10.txt' には 20 (正しい) 行がありません。

配列を作成しないでください。代わりに and を使用するArrayListと、Iteratorそのような問題は発生しません。

于 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;

2 行前 (try ブロックの前) で、この実際の読み取り人数を for ループの上限として使用しますNUM_SALESPEOPLE

于 2013-11-13T23:50:10.903 に答える