简体   繁体   中英

Initializing an array of custom type

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
}

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

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,.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM