Today I had a real head-scratching moment when a completely innocent 'hashtable.put(key,val)' started giving me NullPointerException. Now, since I do know how hashes work, all I checked was - is the table itself null ? Nope, it's good. So what the hell?
Here comes the scary part:
Apparently the usual approach is not good for the Java folk. They don't want no stinking nulls in their precious hashes!put
public V put(K key, V value)
- Maps the specified
key
to the specifiedvalue
in this hashtable. Neither the key nor the value can benull
.The value can be retrieved by calling theget
method with a key that is equal to the original key.
- Parameters:
key
- the hashtable key.value
- the value.- Returns:
- the previous value of the specified key in this hashtable, or
null
if it did not have one.- Throws:
NullPointerException
- if the key or value isnull
.
The reasoning behind the idiocity is this - the default behavior of get() method in hashtable is to return null if key was not found. So when the designers of the API chose that wrong path, they had to follow it up with a wronger path, since they couldn't distinguish between a null from 'no key found' and 'null found as a value'.
I hate people.
No comments:
Post a Comment