简体   繁体   English

在 Java 中迭代数组时出现空指针异常

[英]Nullpointer exception when iterating through array in Java

I have this code:我有这个代码:

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("---------------------");
        }
    }

So I know that the array I initialized has 100 null pointers and at the end, only some of them are filled with Employee objects.所以我知道我初始化的数组有 100 个空指针,最后,只有其中一些被 Employee 对象填充。 I might have an employees array that has 3 Employee objects and 97 null pointers.我可能有一个员工数组,其中包含 3 个 Employee 对象和 97 个空指针。 So how do I fix this?那么我该如何解决这个问题?

You can use an ArrayList instead of the array.您可以使用 ArrayList 而不是数组。 From a performance perspective it is almost the same, but it keeps track of the uninitialized part of the array.从性能角度来看,它几乎相同,但它会跟踪数组的未初始化部分。

Optimally, you would use a list in this case.在这种情况下,最好使用列表。 Like so:像这样:

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("---------------------");
    }
}

Check if employee is not null in the loop:检查employee在循环中是否不为null

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

But using a List<Employee> is what you should consider learning afterwards.但是使用List<Employee>是您之后应该考虑学习的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM