I have two classes:
FirstClass{
private int id;
private SecondClass[] arrayofSecond;
private int ind;
public FirstClass(){}
public FirstClass(int id){
this.id=id;
}
public void addElements(SecondClass e){
arrayofSecond[ind]=e;
ind++;
}
public SecondClass[] getArraySecondClass(){
return arrayofSecond;
}
}
SecondClass{
private int id;
private SecondClass(){}
private SecondClass(int id){
this.id=id;
}
public int getId(){
return id;
}
}
Now I need to get the array that has been filled in the FirtsClass in the Main program, like this:
FirstClass f1=new FirstClass(10);
SecondClass s1=new SecondClass(10);
SecondClass s2=new SeconClass(20);
f1.addElements(s1);
f1.addElements(s2);
FirstClass f=null;
SecondClass s[];
s=f.getArraySecondClass();
for (int i=0;i<s.length;i++){
System.out.println(s[i].getId());
}
The problem is that I get an error of Null pointer exception. I have read that maybe this is not a correct way to do this by the principle of "not talk to strangers", but I do not see another way to make this.
Any help?