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?
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.