So I'm messing around with inhertiance and polymorphism. Everything was going great until I got to the tester where I had to make an array of type employee (my super class). Currently trying to run this program give me this error.
Exception in thread "main" java.lang.NullPointerException
I'm assuming this has something to do with when I declare I have employeeArray = null;. But leaving it out I get an error with putting each employee into the array, it says employee array must be initilized and by default does that by including employeeArray = null;. The book I have on java doesn't really touch on these kinds of arrays and I've been having trouble finding the answer to my troubles online. Any help anyone can offer would be greatly appreciated.
I also tried something like this
Employee [] employeeArray = new Employee[3] ;
This didn't return any errors, but didn't return what I was looking for at all. Is that more like what I need but I've problems in my super and sub classes?
public class EmployeeTest {
public static void main(String[] args){
Employee [] employeeArray = null;
SalariedEmployee employee1 = new SalariedEmployee("Esther", "Smith", "111-111-111", 6, 2011, 2400);
CommissionedEmployee employee2 = new CommissionedEmployee("Nick", "McRae", "222-222-222", 1, 1998, 50000, 0.1);
SalPlusCommEmployee employee3 = new SalPlusCommEmployee("Dan", "Mills", "333-333-333", 3, 2011, 1000, 0.05, 500 );
employeeArray[0] = employee1;
employeeArray[1] = employee2;
employeeArray[2] = employee3;
System.out.println(employeeArray[0].getEmployeeDetails);
System.out.println(employee1.toString()); // call the method from the sub class SalariedEmployee
System.out.println(employee2.toString()); // call the method from the sub class CommissionedEmployee
System.out.println(employee3.toString()); // call the method from the sub class SalPlusCommEmployee
}
Employee [] employeeArray = new Employee[3];is exactly what you need. What do you mean by "didn't return what I was looking for at all"?