View Single Post
  #2 (permalink)  
Old 12-23-2007, 08:07 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
The lines/snippet you posted compile(s) and run(s) okay.
You mentioned an error for the return value of the put method. If I modify your code to explore this
Code:
Hashtable<String, Integer> numbers= new Hashtable<String, Integer>(); numbers.put("one", 1); int m = numbers.put("two", 2); // runtime error Integer n = numbers.put("three", 3); // okay
the line with "int m = ..." (line 7) throws a NullPointerException:
Code:
C:\jexp>java Test Exception in thread "main" java.lang.NullPointerException at Test.main(test.java:7)
j2se 1.5+ has autoboxing which can cause trouble. The put method takes Objects for both key and value. Your Hashtable declaration specifies String(key) and Integer(value) which, according to the javadoc for the class api, will then return an Integer. So if you want a return value from the put method you will need an Integer (vis–a–vis an int primitive data type) to receive it (as shown above).
Reply With Quote