Java 哈希 Table 在检查字符串是否不为空时给出 NullPointerException
Java Hash Table giving NullPointerException when checking if string is not null
我有一个散列table,它是使用节点数组实现的,其中节点包含一个字符串和一个布尔值,用于检查之前是否存在字符串,以便在元素被删除后搜索元素删除。我在一个我不明白我怎么可能的地方得到一个 nullpointerexception,在这一行:
while(T.getElement(place) != null)
这是我执行抛出异常的循环的代码。
获取元素在这里:
public String getElement(int index){
if (index != lastProbed)
probeCount++;
lastProbed = index;
return table[index].str;
}
这是我的节点 class:
class Node {
boolean hadStr;
String str;
public Node() {
str = null;
hadStr = false;
}
}
我不明白我怎么会在我只是检查一个字符串是否为空的地方得到一个空指针。
为什么不
public String getElement(int index){
if (index != lastProbed)
probeCount++;
lastProbed = index;
if (table[index] != null) {
return table[index].str;
}
return null;
}
我有一个散列table,它是使用节点数组实现的,其中节点包含一个字符串和一个布尔值,用于检查之前是否存在字符串,以便在元素被删除后搜索元素删除。我在一个我不明白我怎么可能的地方得到一个 nullpointerexception,在这一行:
while(T.getElement(place) != null)
这是我执行抛出异常的循环的代码。
获取元素在这里:
public String getElement(int index){
if (index != lastProbed)
probeCount++;
lastProbed = index;
return table[index].str;
}
这是我的节点 class:
class Node {
boolean hadStr;
String str;
public Node() {
str = null;
hadStr = false;
}
}
我不明白我怎么会在我只是检查一个字符串是否为空的地方得到一个空指针。
为什么不
public String getElement(int index){
if (index != lastProbed)
probeCount++;
lastProbed = index;
if (table[index] != null) {
return table[index].str;
}
return null;
}