-2

For an assessment I need to create a hash table, which I have done. However, I need to throw an exception when inserting a null key.

The test I ran, which I need to pass, was:

@Test(expected = IllegalArgumentException.class)
public void testNullKey() {
    Hashtable<Boolean> h = new Hashtable<>(10);
    h.put(null, true);
}

Which gives me the error:

java.lang.Exception: Unexpected exception, expected<java.lang.IllegalArgumentException> but was<java.lang.NullPointerException>

Caused by: java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "key" is null
    at Hashtable.hash(Hashtable.java:270)
    at Hashtable.put(Hashtable.java:84)
    at test.TestHT.testNullKey(TestHT.java:52)

Hashtable.hash(Hashtable.java:270) is:

private int hash(String key)
{
    int hashVal = key.hashCode();
    hashVal %= max;

    if (hashVal < 0)

        hashVal += max;

    return hashVal;
}

and Hashtable.put(Hashtable.java:84) is:

public void put(String key, V value)
{
    int h = 0;

    if (getLoadFactor() > maxLoad)
        resize();

    h = probeType == PROBE_TYPE.LINEAR_PROBE ? hash(key) : doubleHash(key);

    if (get(key) != null)
    {
        arr[h] = new Pair(key, value);
    } else
    {
        arr[findEmpty(h, 0, key)] = new Pair(key, value);
        itemCount++;
    }
}

I am at a total loss for throwing an exception when inserting a null key. I've tried adding throw new IllegalArgumentException();and other attemps like that within put, but I can't get it to work.

Does anyone have any ideas or pointers?

Xjh
  • 1
  • 1
  • 1
    When `key` is null, the expression `key.hashCode()` will throw `NullPointerException`, so why are you expecting an `IllegalArgumentException`? --- **Adjust your expectations**, i.e. change your test code to `@Test(expected = NullPointerException.class)`. – Andreas Nov 26 '20 at 23:27
  • 'so why are you expecting an IllegalArgumentException?'. Sadly, I'm not. This is a test we need to pass by our marker. – Xjh Nov 26 '20 at 23:32
  • Then add an `if` statement to throw an `IllegalArgumentException` ***before*** the code tries to execute `key.hashCode()`. What's not obvious about that? – Andreas Nov 26 '20 at 23:36
  • Overloading the name `Hashtable`, even if it's in your own package, is just generally a ***bad idea*** ™. – Jim Garrison Nov 26 '20 at 23:46

1 Answers1

0

OK. So this Hashtable class is your code, and not java.util.Hashtable. (You had me tricked for a minute there ...)

The solution is simple. Explicitly test the key to see if it is null and throw the required exception:

if (key == null) {
    throw new IllegalArgumentException("key is null");
}

I'll leave you to figure out where the best place to put this code is.


The other approach would be to make NullPointerException the expected exception ... but my reading of your question is that that testcase is checking that a requirement is satisfied.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216