aa wrote:
> Hi, I'm a beginner programmer and I keep getting stuck on this one
> problem:
> I try to make a new node:
> TreeNode newTree = new TreeNode(someitem(),somechild());
> but, I keep getting null pointer exceptions on this line:
> Child.setItem(kids.head.listItem);
> does that mean that either Child or kids.head.listItem doesnt exist?
It means that at least one of the references Child, kids, or kids.head
is null. Null is a special value a reference variable has when it does
not point to any object.
> public class TreeNode {
> private Object item;
> private TreeNode Child;
> private TreeNode Sibling;
> public TreeNode(Object item, SList kids) {
> this.item = item;
> Child.setItem(kids.head.listItem); // this one
> }
Child is definitely null. A reference field is null unless it has a
non-null initializer or you have assigned something to it.
However, you will need to be careful about how you initialize it. You
cannot have the only TreeNode constructor unconditionally call itself to
create a new TreeNode for Child or Sibling. If you did that, the
constructor would loop until it ran out of stack space.
Patricia
|