Usually I can solve the errors fine, but I am confused on this one. It gives
me a null pointer exception, which obviously means that I am calling a method
with a null object. But for the sake of it being late, I can't see where that is.
Below shows where the debugger stops and throws an exception :
//display methods
public void printPreOrder(){
if(this == null) return;
System.out.print(toString() + " ");
ListIterator<TreeNode<Type>> itr = _children.iterator(); //throws error here
while(itr.hasNext()){
itr.next().printPreOrder();
}
}
public void printPostOrder(){
if(this == null) return;
System.out.print(toString() + " ");
for(TreeNode<Type> val : _children){ //throws error here
val.printPostOrder();
}
}
Both version throws an null pointer exception. The declaration for _children is
here :
public class TreeNode<Type> implements Position<Type> {
private Type _data;
private TreeNode<Type> _parent;
private DoubleList<TreeNode<Type>> _children;
//... more stuff
//public void printPostOrder();
//public void printPreOrder();
}
DoubleList is for learning purposes, a self made double linked list. I believe
it works for all the things I tested, including the iterator. If you need
to see that as well then ask please. Here is the main class :
public static void main(String[] arg){
//String[] testString = { "one" , "two" , "three" , "four" };
Tree<String> tree = new GeneralTree<String>("root");
tree.printPostOrder();
print();
}
It prints out root, then throws an exception. Can someone see something that
I could not. Thanks.