我不知道为什么我无法做一些简单的操作,只需将泛型类型的对象推入堆栈即可。我决定我需要一些帮助来解决这个问题。这是我有:为什么我不能推入堆栈? Java 6泛型
public BSTSortedListIterator(BSTnode<K> root) {
preOrderTrav(root);
}
任何人有任何的想法:
private void preOrderTrav(BSTnode<K> node) {
if(node != null){
myStack.push(node); //Null pointer exception
while(!myStack.isEmpty()){
myStack.pop();
node = node.getLeft();
myStack.push(node.getRight());
myStack.push(node.getLeft());
}
}
}
这正从我的构造函数叫什么名字?我得到这个错误,顺便说一句:
Exception in thread "main" java.lang.NullPointerException
at BSTSortedListIterator.preOrderTrav(BSTSortedListIterator.java:33)
at BSTSortedListIterator.preOrderTrav(BSTSortedListIterator.java:31)
at BSTSortedListIterator.<init>(BSTSortedListIterator.java:43)
at BSTSortedList.iterator(BSTSortedList.java:130)
at WebDictionary.main(WebDictionary.java:135)
你有没有初始化myStack:
我通过设置像变量解决这个问题?从你发布的代码不清楚。 – 2013-04-25 08:21:51
看起来像你'myStack'实例可能为空 – harsh 2013-04-25 08:22:43