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

标签 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];
    }
}

最佳答案

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

认真反射(reflection)您的设计。为什么这个 EmployeeName 类这么复杂?你为什么不只是有一个字符串字段 name。还是两个字段,firstNamelastName

关于java - 使用类构造函数设置数组值时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18518114/

相关文章:

java - 由于端口 443 正在使用且无法终止,Windows 服务器无法启动 Apache tomcat

c++ - 允许构造函数调用私有(private)方法的默认参数

java - getClass() getResource 返回 null

java - Jboss 4.2类加载

java - 从父 Activity 访问选项卡式 fragment 方法?

java - 如何合并一个音频和视频文件 -- Xuggler

java - Java 中的构造函数覆盖

javascript假实例

android - 应用程序因空指针异常而崩溃

java - 由 : java. lang.NullPointerException 引起:尝试在空对象引用上调用接口(interface)方法