For some reason, whenever I try to push my objects onto a stack, I get a null pointer exception. This is what I have:
private Stack<BSTnode<K>> myStack;
private void preOrderTrav(BSTnode<K> node) {
if(node != null){
myStack.push(node); // I can't step past this step. This is where error occurs
while(!myStack.isEmpty()){
myStack.pop();
node = node.getLeft();
myStack.push(node.getRight());
myStack.push(node.getLeft());
}
}
...//more irreverent code down here
I get this error:
Exception in thread "main" java.lang.NullPointerException
at BSTSortedListIterator.preOrderTrav(BSTSortedListIt erator.java:14)
Does anyone know why I'm unable to push onto my stack?
Thanks