private static Person[] createPersons(int N) {
System.out.println("To be implemented - create "+N+" Person");
Person person[] = new Person[N];
for(int i = 0; i < N; i++){
System.out.println("error1");
int age = 20 + i;
String name = "Mr. Espresso" + i;
final char gender = 'M';
**person[i].setName(name);**
person[i].setAge(age);
person[i].setGender(gender);
}
return person;
public void setAge( int age ) {
this.age = age;
}
/**
* Sets the gender of this person.
*
* @param gender this person's age
*/
public void setGender( char gender ) {
this.gender = gender;
}
/**
* Sets the name of this person.
*
* @param age this person's name
*/
public void setName( String name ) {
this.name = name;
}
I am trying to create a persons (object) array and fill it with values. However it gives a NullPointerException on person[i].setName(name); Can anyone explain me why?
setName/age/gender is referring to a method in the person object class. As seen below.
I've read the other articles, but I don't see it.
You need to create each Person object in your person array.
You probably need above the error line something like
person[i] = new Person();