You are not creating any Person objects. You are creating a Person array with space to hold some Person objects, but I will bet you pounds to pence that you aren’t populating that array with a single Person. It is an empty array.
You might write:
Person[] persons = new Person[3];
persons[0]=new Person();
Now you can call methods of persons[0]
without getting a NullPointerException
. Populate the other 2 elements in the array in the same way.
1
solved Creating a custom class and using it as an object/list [closed]