RSS Feed for This PostCurrent Article

Copying Values Between HashMap and Hashtable

What is the problem with the following code?

String userName = null;
 
if (......) {
    userName = <some value>;
}
Map<String,String> hm = new HashMap<String,String>();
hm.put("username", userName);
.....
 
Map<String,String> ht = new Hashtable<String,String)(hm);

There is a bug in this code. What the the userName remains NULL when you are copying the value to the Hashtable. Hashtable does not allow NULL as part of the value, and depending on the platform and JDK that you are using, you may or may not see any errors at all, and the program will just exit by itself.


Trackback URL


Sorry, comments for this entry are closed at this time.