Custom array type initialization

So, I was busy with inheritance and polymorphism. Everything was great until I got to the tester, where I had to create an array of type employees (my superclass). Currently trying to run this program, give me this error.

Exception in thread "main" java.lang.NullPointerException

I suppose this has something to do when I declare that I have employeeArray = null ;. But leaving this, I get an error message with putting each employee in an array, he says that the array of employees should be initialized, and by default this includes employeeArray = null ;. The book I have in java doesn't really touch on these types of arrays, and I am having trouble finding the answer to my problems on the Internet. Any help anyone can offer would be greatly appreciated.

I also tried something like this

Employee [] employeeArray = new Employee[3] ; 

This did not return any errors, but did not return what I was looking for. This is more like what I need, but am I having problems in super and subclasses?

 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 } 
+4
source share
3 answers

You need to use

Employee [] employeeArray = new Employee[3];

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

But in your case, you don’t have to worry about using the array and giving it a size, you can use an ArrayList instead:

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

As for calling toString() on the employee, you need to override the toString() method for the Employee class, regardless of what you want to use, otherwise you will get the default toString() Object , which displays the class name and hexadecimal representation of the object's hash code .

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

 public String toString() { return this.name + " " + this.firstName ...; } 
+7
source

your

 Employee [] employeeArray = null; 

can cause a null pointer, you must identify the array. add this

 employeeArray = new Employee[3]; 

after this line ,. if SalariedEmployee, CommissionedEmployee are subclasses of Employee, there will be no problems.

0
source

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)); } 
0
source

Source: https://habr.com/ru/post/1400509/


All Articles