5

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
}
3
  • 1
    It looks like 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"? Commented Mar 9, 2012 at 3:04
  • It's returning the subclass and it looks like some hex. SalariedEmployee@a90653 CommissionedEmployee@de6ced SalPlusCommEmployee@c17164 Is what I'm getting. I've got getters and stters for the info that you can see when declaring the employees such as names, SIN, start date and things. I was looking to return all of that information. To be honest I'm just starting with inheritance so it is likely I've made mistakes earlier, lots of thigs were changed trying to get the array to work. Commented Mar 9, 2012 at 3:09
  • ititializing an array with a super-class or supper-interface will gives you more flexibility to add new object into that array,. Commented Mar 9, 2012 at 3:12

3 Answers 3

10

You need to use

Employee [] employeeArray = new Employee[3];

as well as add parentheses at the end of employeeArray[0].getEmployeeDetails()

But in your case, you don't need to worry about using an array and giving it a size, you can use an ArrayList instead, like so:

ArrayList<Employee> employees = new ArrayList<Employee>();
employees.add(new SalariedEmployee(...));
employees.add(new CommissionnedEmployee(...));
...

As for calling toString() on an employee, you need to property override the toString() method for the Employee class, with whatever you want the ouput to be, otherwise you will get the default toString() of the Object class, which outputs the class name and the hexadecimal representation of the hash code of the object.

Your Employee class should have this method (something like it):

public String toString() {
    return this.name + " " + this.firstName ...;
}
Sign up to request clarification or add additional context in comments.

2 Comments

unless getEmployeeDetails is some kind of public variable but it does sound like a method call.
Wonderful, that was very helpful. Thank you, now I'm just getting nulls for everything when looking for employee details. Hopefully this will be an easier fix. Thanks again!
0

your

Employee [] employeeArray = null;

may cause a null pointer, you should identify the array,. add this

employeeArray = new Employee[3];

after that line,. if SalariedEmployee, CommissionedEmployee are sub classes of Employee, there will be no problem,.

Comments

0

This code works, please check your classes.

public static void main(String[] args) {

    Object[] objets = new Object[3];

    String s1 = new String("s1");

    String s2 = new String("s2");

    String s3 = new String("s3");

    objets[0] = s1;
    objets[1] = s2;
    objets[2] = s3;

    System.out.println(Arrays.asList(objets));

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.