Java 在二叉树上迭代时出现NullPointerException
现在我不确定是否有人能真正帮助我,因为这是一个相当大的代码量,但任何帮助都将不胜感激。这是我的相关代码:Java 在二叉树上迭代时出现NullPointerException,java,nullpointerexception,binary-tree,Java,Nullpointerexception,Binary Tree,现在我不确定是否有人能真正帮助我,因为这是一个相当大的代码量,但任何帮助都将不胜感激。这是我的相关代码: public class BTree implements Iterable<String> { /** Left child */ BTree left; /** Right Child */ BTree right; /** Comparator to use for sorting */ Comparator<Strin
public class BTree implements Iterable<String> {
/** Left child */
BTree left;
/** Right Child */
BTree right;
/** Comparator to use for sorting */
Comparator<String> comp;
/** Parent node */
BTree parent;
/** String stored in this leaf */
String s;
/** # of iterators currently working on it */
int active = 0;
/** Size of the BTree */
int size;
public void build(Iterable<String> iter, int numStrings) {
if (this.active > 0) {
throw new ConcurrentModificationException();
}
else {
Iterator<String> itr = iter.iterator();
while (numStrings != 0 && itr.hasNext()) {
String s = itr.next();
if (!this.contains(s)) {
this.insert(s);
this.size++;
numStrings--;
}
}
}
}
/**
* Inserts the string into the given BTree
* @param str - String to insert
*/
private void insert(String str) {
if (this.s.equals("")) {
this.s = str;
}
else if (this.comp.compare(str, this.s) > 0) {
if (this.right == null) {
BTree bt = BTree.binTree(this.comp);
bt.s = str;
this.right = bt;
bt.parent = this;
}
else {
this.right.insert(str);
}
}
else if (this.comp.compare(str, this.s) < 0) {
if (this.left == null) {
BTree bt = BTree.binTree(this.comp);
bt.s = str;
this.left = bt;
bt.parent = this;
}
else {
this.left.insert(str);
}
}
}
private class BTreeIterator implements Iterator<String> {
/** Current BTree being iterated over */
BTree current;
/** How many next() calls have there been */
int count;
/** Size of the BTree */
int max;
/** Constructor for BTreeIterator
* @param current
*/
BTreeIterator(BTree current) {
this.current = current;
this.count = 0;
this.max = current.size;
active++;
}
/** Returns true if there is another string to iterate over
* @return boolean
*/
public boolean hasNext() {
if (this.count != this.max) {
return true;
}
else {
active--;
return false;
}
}
/**
* Returns the next string in the iterator
* @return String
*/
public String next() {
if (this.count == 0) {
this.count++;
current = this.current.getLeftMost();
if (this.current.s.equals("")) {
throw new NoSuchElementException();
}
return this.current.s;
}
else if (this.current.right != null) {
this.current = this.current.right.getLeftMost();
this.count++;
return this.current.s;
}
else {
BTree tree = this.current;
while (tree.parent.right == tree) {
tree = tree.parent;
}
this.current = tree.parent;
this.count++;
return this.current.s;
}
}
/** Throws an exception since we aren't removing anything from the trees
*/
public void remove() {
throw new UnsupportedOperationException();
}
}
}
更奇怪的是,对于同一个199个单词的文件,它可以与比较器配合使用,但一旦我将其累积到200个单词,它就会崩溃
编辑:我已经确定异常是由于代码试图引用
tree.parent.right
,而tree.parent
为null
,但我不明白它为什么要这样做。据我所知,我的代码永远不应该尝试调用空的树。parent
,正如我在下面的评论所解释的那样。好吧,您实际上没有显示足够的代码,但是,您的BTree的根节点呢?根节点中的BTree.parent
的值是多少<代码>空值
如果根节点的父节点为空,则在此while循环中,很可能:
while (tree.parent.right == tree) {
tree = tree.parent;
}
。。。将到达根节点,
树
将设置为树。父级
,即空
,然后while循环测试将失败,因为tree.parent.right
将尝试取消对空指针的引用。将try&catch添加到比较器中,并在catch中打印o1
和o2
的值,这样您就可以找到您的“角落案例”。唯一需要考虑的是,您不会处理o1
或o2
为null
(似乎s1
在某种程度上是null
,并且您在最后一行有一个NPE)的情况。根父级为null,但代码永远不会到达该点。最顶端的节点都有一个右子节点,在这种情况下,else if(this.current.right!=null)
应该捕获它。如果它没有正确的子节点,那么它应该是最后一个要迭代的节点,hasNext()
应该为false,阻止它尝试调用tree.parent。
while (tree.parent.right == tree) {
tree = tree.parent;
}