beginner question about null pointer exceptions

Discussion in 'Java' started by aa, Jul 24, 2006.

  1. aa

    aa Guest

    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;
    }
     
    aa, Jul 24, 2006
    #1
    1. Advertising

  2. aa

    Moiristo Guest

    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 could mean that Child, kids, or head does not exist.
     
    Moiristo, Jul 24, 2006
    #2
    1. Advertising

  3. aa

    Moiristo Guest

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


    Looking at your code, you've probably forgotten to initialize Child
     
    Moiristo, Jul 24, 2006
    #3
  4. 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.

    --
    Papastefanos Serafeim
    ? "aa" <> ?????? ??? ??????
    news:...
    > 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;
    > }
    >
     
    Papastefanos Serafeim, Jul 24, 2006
    #4
  5. 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
     
    Patricia Shanahan, Jul 24, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,693
    Mike Schilling
    Mar 29, 2006
  2. Tennessee James Leeuwenburg

    null pointer exceptions

    Tennessee James Leeuwenburg, Jul 18, 2003, in forum: Python
    Replies:
    1
    Views:
    339
    Erik Max Francis
    Jul 18, 2003
  3. Alan

    Null pointer exceptions

    Alan, Dec 25, 2007, in forum: Java
    Replies:
    4
    Views:
    563
    Roedy Green
    Dec 28, 2007
  4. aneuryzma
    Replies:
    3
    Views:
    716
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    445
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page