-1

I've been writing a HashTable for an assignment and I've been given tests to run and pass. As of now I'm passing all the tests apart from 3. But as far as I can tell from my own tests all the methods work fine. Apart from my Exception on the remove() method.

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);
    }
}

When I run this test I get an error and I'm not sure what it is.

//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;        }
}

I'll also link my MapException Class here:

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);

    }

}

The error is:

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)

Friibo
  • 1
  • 3
  • The problem is for sure that the hashTable[i] is null. As long we do not know what linearProbing() is doing and how big is your hashTable array we cannot help. And: a string is compared for equality by the equals() method and not by a '=='! – Heri Oct 24 '16 at 19:40
  • Thanks for this reply, it was an obvious error i completely looked over. Once i made this change it fixed another problem i had with my tests. Thanks :) – Friibo Oct 25 '16 at 12:58

1 Answers1

0
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)

The NullPointerException means, generally, that you're trying to access an Object that doesn't really exist (or is Null).

With the line numbers you've added in the problem is clear -- the remove() checks

hashTable[i].getKey() == key

hashtable[i] returns Null, so getKey fails, throwing the null reference error.

Try:

...
public void remove(String key) throws MapException{
    int i = this.linearProbing(key);

    (hash) entry = hashTable[i];

    if((entry !=null) && (entry == key)){
        numberOfEntries--;
 ...

This will check to see if the entry exists in the table before operating on it.

UrhoKarila
  • 354
  • 7
  • 26
  • I added the lines in which the errors are pointing at – Friibo Oct 24 '16 at 18:38
  • It seems to fail when i try to remove a key that doesn't exist in the HashTable. It should throw an Exception. i checked to see if 'hashTable[i].getKey()' returned an acceptable answer and it does. – Friibo Oct 24 '16 at 18:43
  • It *is* throwing an exception, but not one that you're catching. Your *try ... catch* only catches MapExceptions, but attempting to access an element that doesn't exist throws a NullPointerException. It isn't what you've said to handle, so it doesn't get caught. – UrhoKarila Oct 24 '16 at 18:45
  • Worth noting, when I wrote "(hash)" it's because I didn't know what type of objects are stored in the array. You'll need to replace that with the correct type. – UrhoKarila Oct 24 '16 at 18:48
  • Cheers man, got it working. Makes sense now :) thanks for the help – Friibo Oct 24 '16 at 18:58