Java BinarySearchTreeMap中的空指针
我正在完成一个BinarySearchTreeMap类,它听起来就是这样的:它是一个用二进制搜索树实现的映射 我似乎得到了一个空指针异常,我就是无法控制它。我想在其他类似情况下也会发生这种情况。我得到了有问题的方法,我的逻辑似乎是正确的,因此,如果有人有任何关于为什么会有空指针的提示或提示,以及我将来如何防止这些,请让我知道 下面是我正在创建的方法-在外部节点插入条目:Java BinarySearchTreeMap中的空指针,java,map,insert,nullpointerexception,binary-search-tree,Java,Map,Insert,Nullpointerexception,Binary Search Tree,我正在完成一个BinarySearchTreeMap类,它听起来就是这样的:它是一个用二进制搜索树实现的映射 我似乎得到了一个空指针异常,我就是无法控制它。我想在其他类似情况下也会发生这种情况。我得到了有问题的方法,我的逻辑似乎是正确的,因此,如果有人有任何关于为什么会有空指针的提示或提示,以及我将来如何防止这些,请让我知道 下面是我正在创建的方法-在外部节点插入条目: /** Auxiliary method for inserting an entry at an external
/** Auxiliary method for inserting an entry at an external node. Inserts e
* at v, expanding v to be internal with empty external children, and then
* returns e. */
protected Entry<K,V> insertAtExternal(Position<Entry<K,V>> v, Entry<K,V> e) throws InvalidEntryException{
if(isInternal(v)) throw new InvalidEntryException("You tried to insert an external at an internal node!");
replaceEntry(v,e); //replace the node at the given position
insertLeft(v,null); //insert a dummy external node to the left of it
insertRight(v,null); //insert a dummy external node to the right of it
numEntries++; //since v is now internal, include it in the numEntries
return e;
}
protected V replaceEntry(Position <Entry<K,V>> pos, Entry<K,V> ent) {
((BSTEntry<K,V>) ent).pos = pos;
return replace(pos, ent).getValue();
}
return语句是给我带来麻烦的行。
任何帮助都将不胜感激。
克劳迪娅
编辑:
以下是替换方法的实现方式:
replace(Position<E> v, E o)
throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
E temp = v.element();
vv.setElement(o);
return temp;
}
可以显示堆栈跟踪吗?replace方法是什么样子的?