Results 1 to 1 of 1
- 11-29-2010, 01:27 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 44
- Rep Power
- 0
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:
somewhere in my main method i am creating the node: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 this.children.add(toInsert); toInsert.setParentNode(this); toInsert.children=null; } else { this.children.add(toInsert); } return this; }
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.
any help suggestions would be welcomed as i can't figure out the cause of the NullPointer unless the "this" reference is wrong.Java Code:hello / \ jack nancy | | John jacobLast edited by f1gh; 11-29-2010 at 04:10 AM.
Similar Threads
-
help with determing if a node is duplicate and if so prevent from adding to list
By jputneyCS in forum New To JavaReplies: 6Last Post: 10-09-2010, 12:30 AM -
Linked List node class help!!
By SteroidalPsycho in forum New To JavaReplies: 0Last Post: 05-03-2010, 01:21 AM -
Adding a node with a TreeViewer; what's wrong?
By Rodrigo Braz in forum SWT / JFaceReplies: 1Last Post: 04-24-2009, 07:43 AM -
Adding/removing nodes to tree under TreeViewer
By Rodrigo Braz in forum SWT / JFaceReplies: 0Last Post: 04-20-2009, 01:02 AM -
linked list nodes all refernce same item.
By yllawwally in forum New To JavaReplies: 0Last Post: 12-18-2007, 08:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks