Every time when I make a class and other class that will be an array where i will store the first class objects and i call a method ,i receive a nullPointerEception..
ex:
public class person {
private String name,int age;
public Class()//constructor
public void print() {
System.out.println(name);
System.out.println(name);
//.........others methods
}
}
public class personList {
person[] pl = new person[10];
int i=0;
public void add(person p) {
pl[i]=p;
i++
}
public void print() {
for(int j=0;j<pl.length;j++)
{
pl[j].print();
}
}
}
The error is here: pl[j].print();
The object p[j] isn't null ,because I initiate in Main file(p=new person("Maxim",17),pl.add(p)).
Even if I intializate from Main like this p[0]=.....,p[1]=... I receive the same error.
What have I done wrong?