Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 844
Sh0gun Avatar asked Mar 09 '12 03:03

Sh0gun


People also ask

How do you initialize a custom array in Java?

We can declare and initialize arrays in Java by using a new operator with an array initializer. Here's the syntax: Type[] arr = new Type[] { comma separated values }; For example, the following code creates a primitive integer array of size 5 using a new operator and array initializer.

How do you create a custom array?

In this article we will see that how we can create arrays of custom types. We can achieve it by using custom types. We all know that array is a reference type i.e. memory for an array is allocated in a heap. Sometimes we need to declare arrays of custom types rather than arrays of predefined types ( int or string etc).

Which type is correct for initializing an array?

The initializer for an array is a comma-separated list of constant expressions enclosed in braces ( { } ). The initializer is preceded by an equal sign ( = ). You do not need to initialize all elements in an array.


1 Answers

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 ...;
}
like image 170
JRL Avatar answered Sep 20 '22 20:09

JRL