Sign in to follow this  
Concentrate

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 Fenrisulvur
Also, 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 this post


Link to post
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 Fenrisulvur
Also, 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.

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this