使用类构造函数设置数组值时出现NullPointerException

时间:2013-08-29 18:09:35

标签: java constructor nullpointerexception

我对java非常陌生,并且非常接近完成我一直试图完成的项目很长一段时间。每当我尝试运行代码时。一旦调用构造函数,它就会给出一个空指针异常。我的代码列在下面,任何帮助将不胜感激。

主要课程:

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args)
    {
        Scanner Input = new Scanner(System.in);

        System.out.println("Enter the number of employees to register.");
        int arraySize = Input.nextInt();
        Input.nextLine();

        Employee employee = new Employee(arraySize);

        String namesTemp;
        String streetTemp;
        String cityTemp;
        String stateTemp;
        String zipCodeTemp;
        String dateOfHireTemp;

        for(int x = 0; x < arraySize; x++)
        {
            System.out.println("Please enter the name of Employee " + (x + 1));
            namesTemp = Input.nextLine();
            System.out.println("Please enter the street for Employee " + (x + 1));
            streetTemp = Input.nextLine();
            System.out.println("Please enter the city of Employee " + (x + 1));
            cityTemp = Input.nextLine();
            System.out.println("Please enter the state of Employee " + (x + 1));
            stateTemp = Input.nextLine();
            System.out.println("Please enter the zip code of Employee " + (x + 1));
            zipCodeTemp = Input.nextLine();
            System.out.println("Please enter the date of hire for Employee " + (x + 1));
            dateOfHireTemp = Input.nextLine();
            employee.addEmployee(x, namesTemp, streetTemp, cityTemp, stateTemp, zipCodeTemp, dateOfHireTemp);
            System.out.println("The employee ID for employee " + (x + 1) + " is " + (x + 1));
        }

        for(int x = 0; x < arraySize; x++)
        {
            String info[] = employee.getEmployeeInfo(x);

            System.out.println("Employee ID: " + (x + 1));
            System.out.println("Name: " + info[0]);
            System.out.println("Address: " + info[1]);
            System.out.println("Date of Hire: " + info[2]);
        }
    }
}

员工类:

public class Employee 
{
    private EmployeeName name;
    private EmployeeAddress address;
    private EmployeeDateOfHire hireDate;

    public Employee(int arraySize)
    {

    }

    public void addEmployee(int x, String name, String street, String city, String state, String zipCode, String hireDate)
    {
        this.name.setName(x, name);
        this.address.setAddress(x,  street,  city,  state,  zipCode);
        this.hireDate.addDateOfHire(x,  hireDate);
    }

    public String[] getEmployeeInfo(int x)
    {
        String info[] = new String[3];
        info[0] = name.getName(x);
        info[1] = address.getAddress(x);
        info[2] = hireDate.getDateOfHire(x);
        return info;
    }
}

编辑 -

以下是我编写数据类的方法。它们都以相同的格式编写。

名称等级:

public class EmployeeName 
{
    private String names[];

    public void setArray(int x)
    {
        String array[] = new String[x];
        this.names = array;
    }

    public void setName(int x, String name)
    {
        this.names[x] = name;
    }

    public String getName(int x)
    {
        return this.names[x];
    }
}

1 个答案:

答案 0 :(得分:3)

addEmployee()方法

public void addEmployee(int x, String name, String street, String city, String state, String zipCode, String hireDate)
    this.name.setName(x, name);
    this.address.setAddress(x,  street,  city,  state,  zipCode);
    this.hireDate.addDateOfHire(x,  hireDate);
}

您尚未初始化name或其他字段。默认情况下,实例字段将初始化为null。尝试取消引用null会导致NullPointerException

您应该初始化这些字段,例如在构造函数

public Employee(int arraySize)
{
    this.name = new EmployeeName();
    this.address = new EmployeeAddress();
    this.hireDate = new EmployeeDateOfHire();
}

我不知道那些类是什么样的。

看到

private String names[];

public void setArray(int x)
{
    String array[] = new String[x];
    this.names = array;
}

public void setName(int x, String name)
{
    this.names[x] = name;
}

您致电setName(),这也会引发NullPointerException,因为您试图取消引用names,但它是null。你必须首先调用setArray(),但那也会失败,因为如果x为0,你将创建一个大小为0的数组,然后尝试访问索引0处的元素,这将不存在。如果x为1,您将创建大小为1的数组,但尝试访问索引1(第二个元素)的元素,这会抛出IndexOutOfBoundsException

认真重新考虑你的设计。为什么这个EmployeeName类如此复杂?你为什么不只有一个字符串字段name。或两个字段firstNamelastName