beginner question about null pointer exceptions

A

aa

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?



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
}



public class SList {
public SListNode head;
public SList() {
head = null;
}
public SList(SListNode head) {
this.head = head;
}


public class SListNode {
public Object listItem;
public SListNode next;
public SListNode(Object item) {
this.listItem = item;
}
 
M

Moiristo

aa said:
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 could mean that Child, kids, or head does not exist.
 
P

Papastefanos Serafeim

I don't see a constructor in TreeNode...
Before using Child.setItem(...), you have to instantiate the
Child object by using TreeNode Child = new TreeNode();

Also, another hint: Don't capitalize object names. It'd be
better to use private TreeNode child, private treeNode
sibling.
 
P

Patricia Shanahan

aa said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top