在Java中遍历数组时出现Nullpointer异常

时间:2019-07-07 18:33:26

标签: 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个空指针。那我该如何解决呢?

3 个答案:

答案 0 :(得分:1)

您可以使用ArrayList代替数组。从性能的角度来看,它几乎是相同的,但是它可以跟踪数组的未初始化部分。

答案 1 :(得分:1)

在这种情况下,最好使用列表。 像这样:

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

答案 2 :(得分:1)

检查循环中的INDEX&MATCH是否不是employee

null

但是之后应该考虑使用public static void printAllEmployees(Employee[] employees) { for(Employee employee : employees) { if(employee!=null) { System.ou.println...