Alexander Gulea
Alexander Gulea

Reputation: 39

NullPointerException when using class constructor to set array value

I am extremely new to java and am very close to finishing a project I have been trying to finish for a long time. Whenever I try to run the code. It gives a null pointer exception as soon as the constructer is called. My code is listed below and any help would be appreciated.

Main class:

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]);
        }
    }
}

Employee class:

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;
    }
}

EDIT--

Here is how I wrote my data classes. They are all written in the same format.

Name Class:

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

Upvotes: 1

Views: 2170

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279910

In the addEmployee() method

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

you haven't initialized name or the other fields. By default, instance fields will be initialized to null. Trying to dereference null will cause a NullPointerException.

You should initialize those fields, for example in your Constructor

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

I don't know what those classes look like.

Seeing

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;
}

You call setName() which would also throw a NullPointerException because you're trying to dereference names but it's null. You would have to call setArray() first but then that would fail too because if x is 0, you will create an array of size 0 but then try to access the element at index 0, which would not exist. If x was 1, you would create array of size 1 but try to access the element at index 1 (second element), which would throw an IndexOutOfBoundsException.

Seriously rethink your design. Why is this EmployeeName class so complicated? Why don't you just have a String field name. Or two fields, firstName and lastName?

Upvotes: 3

Related Questions