Recursive Class throws NullPointerException
Hi all,
I literally just started with Java and I am experimenting with OOP, but this puzzles me:
I have something that resembles the following (plus getters, setters etc):
Code:
public class person {
public String name;
public person father;
public ArrayList<person> children;
}
Now I wanted to create relationships:
Code:
person p1 = new person();
p1.setName("Lucy");
person p2 = new person();
p2.setName("Fred");
[B]p1.father= p2;
p2.children.add(p1);[/B]
However, here is where I get the NullPointerExceptions.
It doesn't seem to work this way, and I just assume I am not yet in the right mind set for OOP to understand what's wrong here.
btw, using a setter for the first bold line:
doesn't work either. same exception.
Can anyone enlighten me, please?