簡體   English   中英

在 Java 中迭代數組時出現空指針異常

[英]Nullpointer exception when iterating through array in Java

我有這個代碼:

public class EmployeeDataEntry {
    public static void main(String args[]) throws IOException {
        Employee[] employees = new Employee[100];
        System.out.println("Welcome to the Employee Data Entry System. You can enter up to 100 employees at at time.");

        for(int i = 0; i < employees.length ; i++) {
            while (!command.equals("p")) {
                Employee employee = new Employee();

     ...(some data processing stuff)
                // add the employee to the employees list.
                employees[i] = employee;

            }
        }

        // print out all the employees and their data. The check is to prevent null point exception.
        if ((employees[0] instanceof Employee)) {
            printAllEmployees(employees);
        } else {
            System.out.println("There are no employees");
        }
    }

    public static void printAllEmployees(Employee[] employees) {
        for(Employee employee : employees) {
            System.out.println("Employee Employee Number: " + employee.getEmployeeNumber());
            System.out.println("Employee Name: " + employee.getName());
            System.out.println("Employee Address: " + employee.getAddress());
            System.out.println("Employee Hire Date: " + employee.getHireDate());
            System.out.println("---------------------");
        }
    }

所以我知道我初始化的數組有 100 個空指針,最后,只有其中一些被 Employee 對象填充。 我可能有一個員工數組,其中包含 3 個 Employee 對象和 97 個空指針。 那么我該如何解決這個問題?

您可以使用 ArrayList 而不是數組。 從性能角度來看,它幾乎相同,但它會跟蹤數組的未初始化部分。

在這種情況下,最好使用列表。 像這樣:

public class EmployeeDataEntry {
public static void main(String args[]) throws IOException {
    List<Employee> employees = new ArrayList<Employee>();
    System.out.println("Welcome to the Employee Data Entry System. You can enter up to 100 employees at at time.");

    for(int i = 1; i < 100 ; i++) {
        while (!command.equals("p")) {
            Employee employee = new Employee();

 ...(some data processing stuff)
            // add the employee to the employees list.
            employees.add(employee);

        }
    }

    // print out all the employees and their data. The check is to prevent null point exception.
    if (employees.isEmpty()) {
        printAllEmployees(employees);
    } else {
        System.out.println("There are no employees");
    }
}

public static void printAllEmployees(Listy<Employee> employees) {
    for(Employee employee : employees) {
        System.out.println("Employee Employee Number: " + employee.getEmployeeNumber());
        System.out.println("Employee Name: " + employee.getName());
        System.out.println("Employee Address: " + employee.getAddress());
        System.out.println("Employee Hire Date: " + employee.getHireDate());
        System.out.println("---------------------");
    }
}

檢查employee在循環中是否不為null

public static void printAllEmployees(Employee[] employees) {
  for(Employee employee : employees) {
    if(employee!=null) {
      System.ou.println...

但是使用List<Employee>是您之后應該考慮學習的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM