have a class with an array of objects and getting a null poi

have a class with an array of objects and getting a null pointer exception.

How do I fix the nullpointerexception on this array of objects? I do not need the EmployeeDriver class change or altered as this was given by the teacher.

I need to understand why and how to fix this error with EmployeeList not EmployeeDriver.

When addEmployee is called I am getting this error.

Exception in thread \"main\" java.lang.NullPointerException
at EmployeeList.addEmployee(EmployeeList.java:42)
at EmployeeDriver.main(EmployeeDriver.java:124)

Here is the Driver class

Solution

note: constructor does not have return type

public class EmployeeList {

private final int EMPLOYEES_MAX = 50;
private int numEmployees;
private Employee[] employees;

public EmployeeList() {
employees = new Employee[EMPLOYEES_MAX];
numEmployees = 0;
}

public boolean addEmployee(int empType, String first, String last, char gen, int empNum, boolean full, double amount) {
if (empType == 1) {
employees[numEmployees] = new HourlyEmployee(first, last, gen, empNum, full, amount); // this is line 42
} else if (empType == 2) {
employees[numEmployees] = new SalaryEmployee(first, last, gen, empNum, full, amount);
} else {
employees[numEmployees] = new CommissionEmployee(first, last, gen, empNum, full, amount);
}

if (numEmployees <= EMPLOYEES_MAX) {
numEmployees++;
} else {
System.out.print(\"Cannot add emplyee, list is full\");
return false;
}
return true;
}
}

have a class with an array of objects and getting a null pointer exception. How do I fix the nullpointerexception on this array of objects? I do not need the Em

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site