Results 1 to 1 of 1
  1. #1
    f1gh is offline Banned
    Join Date
    Nov 2010
    Rep Power

    Default adding children to a node that has List of Nodes

    Hi All:

    Okay i am trying to add elements to a List<Node <T>>, i.e. the list is a compilation of generic Nodes. Each node has a <T> data field, has a List<Node <T>> children list, and has Node<T> parent. Here is the code i am trying to write but keep getting NullPointerException:

    Java Code:
        public Node<T> addChild(T child) {
            Node toInsert = new Node(child);
           int numchild = getNumberOfChildren();
           System.out.println("This node has: "+numchild);
           //basically if this node doesn't have a parent Node
           if(this.parent ==null){
                //this line of code is where i am getting NullPointerException
           return this;
    somewhere in my main method i am creating the node:
    Node<String> s1 = new Node<String>(new String("hello"));
    Node<String> s2 = s1.addChild(new String("jack"));

    and than ofcourse i further add nodes and childs so that end i almost have a list that branches out like a tree i.e.
    Java Code:
              /      \
            jack    nancy
             |       |
            John    jacob
    any help suggestions would be welcomed as i can't figure out the cause of the NullPointer unless the "this" reference is wrong.
    Last edited by f1gh; 11-29-2010 at 05:10 AM.

Similar Threads

  1. Replies: 6
    Last Post: 10-09-2010, 01:30 AM
  2. Linked List node class help!!
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 05-03-2010, 02:21 AM
  3. Adding a node with a TreeViewer; what's wrong?
    By Rodrigo Braz in forum SWT / JFace
    Replies: 1
    Last Post: 04-24-2009, 08:43 AM
  4. Adding/removing nodes to tree under TreeViewer
    By Rodrigo Braz in forum SWT / JFace
    Replies: 0
    Last Post: 04-20-2009, 02:02 AM
  5. linked list nodes all refernce same item.
    By yllawwally in forum New To Java
    Replies: 0
    Last Post: 12-18-2007, 09:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts