HashTable 中的 Remove() 方法测试失败
Remove() method in HashTable failing tests
我一直在为一项作业编写哈希表,我已经接受了 运行 的测试并通过了。
截至目前,我通过了除 3 以外的所有测试。但据我自己的测试所知,所有方法都可以正常工作。除了我在 remove()
方法上的异常。
public void remove(String key) throws MapException{
int i = this.linearProbing(key);
if(hashTable[i].getKey() == key){ //line 76
numberOfEntries--;
numberOfRemovals++;
hashTable[i] = this.DEFUNCT;
}
else{
String e = "No entry of this key found";
throw new MapException(e);
}
}
当我 运行 这个测试时,我得到了一个错误,我不确定它是什么。
//Remove a non-existent entry. Should throw an Exception
private static boolean test4() throws MapException {
StringHashCode sHC = new StringHashCode();
float maxLF = (float) 0.5;
HashTableMap h = new HashTableMap(sHC,maxLF);
try {
h.insert("R3C1");
} catch (MapException e1) {
return false;
}
try {
h.remove("R6C8"); //line 117
return false;
} catch (MapException e) {
return true; }
}
我也会 link 我的 MapException Class 在这里:
import java.lang.Exception;
public class MapException extends Exception {
public MapException(){
}
public MapException(String exception){
super(exception);
}
public MapException(String exception, Throwable throwable) {
super(exception, throwable);
}
public MapException(Throwable throwable) {
super(throwable);
}
}
错误是:
Exception in thread "main" ***Test 1 failed Test 2 succeeded Test 3 succeeded java.lang.NullPointerException at HashTableMap.remove(HashTableMap.java:73) at TestHashTableMap.test4(TestHashTableMap.java:117) at TestHashTableMap.main(TestHashTableMap.java:24)
Exception in thread "main" ***Test 1 failed Test 2 succeeded Test 3 succeeded
java.lang.NullPointerException
at HashTableMap.remove(HashTableMap.java:73)
at TestHashTableMap.test4(TestHashTableMap.java:117)
at TestHashTableMap.main(TestHashTableMap.java:24)
NullPointerException 通常意味着您正在尝试访问一个并不真正存在(或为 Null)的对象。
根据您添加的行号,问题就很清楚了——remove() 检查
hashTable[i].getKey() == key
hashtable[i] returns Null,所以 getKey 失败,抛出空引用错误。
尝试:
...
public void remove(String key) throws MapException{
int i = this.linearProbing(key);
(hash) entry = hashTable[i];
if((entry !=null) && (entry == key)){
numberOfEntries--;
...
这将在对其进行操作之前检查条目是否存在于 table 中。
我一直在为一项作业编写哈希表,我已经接受了 运行 的测试并通过了。
截至目前,我通过了除 3 以外的所有测试。但据我自己的测试所知,所有方法都可以正常工作。除了我在 remove()
方法上的异常。
public void remove(String key) throws MapException{
int i = this.linearProbing(key);
if(hashTable[i].getKey() == key){ //line 76
numberOfEntries--;
numberOfRemovals++;
hashTable[i] = this.DEFUNCT;
}
else{
String e = "No entry of this key found";
throw new MapException(e);
}
}
当我 运行 这个测试时,我得到了一个错误,我不确定它是什么。
//Remove a non-existent entry. Should throw an Exception
private static boolean test4() throws MapException {
StringHashCode sHC = new StringHashCode();
float maxLF = (float) 0.5;
HashTableMap h = new HashTableMap(sHC,maxLF);
try {
h.insert("R3C1");
} catch (MapException e1) {
return false;
}
try {
h.remove("R6C8"); //line 117
return false;
} catch (MapException e) {
return true; }
}
我也会 link 我的 MapException Class 在这里:
import java.lang.Exception;
public class MapException extends Exception {
public MapException(){
}
public MapException(String exception){
super(exception);
}
public MapException(String exception, Throwable throwable) {
super(exception, throwable);
}
public MapException(Throwable throwable) {
super(throwable);
}
}
错误是:
Exception in thread "main" ***Test 1 failed Test 2 succeeded Test 3 succeeded java.lang.NullPointerException at HashTableMap.remove(HashTableMap.java:73) at TestHashTableMap.test4(TestHashTableMap.java:117) at TestHashTableMap.main(TestHashTableMap.java:24)
Exception in thread "main" ***Test 1 failed Test 2 succeeded Test 3 succeeded
java.lang.NullPointerException
at HashTableMap.remove(HashTableMap.java:73)
at TestHashTableMap.test4(TestHashTableMap.java:117)
at TestHashTableMap.main(TestHashTableMap.java:24)
NullPointerException 通常意味着您正在尝试访问一个并不真正存在(或为 Null)的对象。
根据您添加的行号,问题就很清楚了——remove() 检查
hashTable[i].getKey() == key
hashtable[i] returns Null,所以 getKey 失败,抛出空引用错误。
尝试:
...
public void remove(String key) throws MapException{
int i = this.linearProbing(key);
(hash) entry = hashTable[i];
if((entry !=null) && (entry == key)){
numberOfEntries--;
...
这将在对其进行操作之前检查条目是否存在于 table 中。