# NullPointerException[ JAVA ]

## Recommended Posts

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.

##### Share on other sites
Well, obviously _children is null. I can't see you initializing the variable anywhere, and as long as you don't do that, it will be null. You need something like:

private DoubleList<TreeNode<Type>> _children = new DoubleList<TreeNode<Type>>();

##### Share on other sites
Well, we haven't seen the stack trace for the exception, so it could be internal to the iterator() call. So, if _children is being initialized with an object value, then yes, we will need to see the source for that DoubleList class.
If we're looking at the root of the stack trace, though, then yes, the collection isn't being initialized.

Also, what's with all the people who seem to think that "Java" is an acronym?

##### Share on other sites
Sounds to me that the problem is (or at least is likely to be) what Oysterman described. I would also recommend that you stick to naming generic types with a single letter, since that's the convention.

Quote:
 Original post by FenrisulvurAlso, what's with all the people who seem to think that "Java" is an acronym?

It's not a trend that started yesterday, unfortunately.

##### Share on other sites
Quote:
Original post by lightbringer
Sounds to me that the problem is (or at least is likely to be) what Oysterman described. I would also recommend that you stick to naming generic types with a single letter, since that's the convention.

Quote:
 Original post by FenrisulvurAlso, what's with all the people who seem to think that "Java" is an acronym?

It's not a trend that started yesterday, unfortunately.

Thanks guys. It was really late. _children was not initialized. Anyway, I am use
to C++, thats why I use Type instead of a single letter. Thanks again.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628307
• Total Posts
2981972

• 9
• 13
• 11
• 12
• 11