Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-23-2007, 01:18 PM
Member
 
Join Date: Dec 2007
Posts: 5
new_1 is on a distinguished road
Hashtable-put method
Hi,
I wrote a code, below is a part of it(using NetBeans IDE 6.0)
Code:
............. Hashtable<String, Integer> numbers= new Hashtable<String, Integer>(); numbers.put("one", 1); numbers.put("two", 2); numbers.put("three", 3); ............
Problem is the following:
-in tutorials i read put() method just puts the parameter in the table(we didnt have to write n=numbers.put("one", Integer(1)) ).
-when i wrote the above code, there was an error telling me that put() method returns an Integer.
Help me understand please.
Thanks.

Last edited by new_1 : 12-23-2007 at 01:21 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-23-2007, 08:07 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
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).
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Working with Key-Value Pairs in a Hashtable Java Tip java.lang 0 04-12-2008 09:44 PM
Hashtable example Java Tip Java Tips 0 02-15-2008 09:43 AM
HashTable problem jhetfield18 New To Java 2 12-16-2007 11:27 PM
HashTable vs HashMap bugger New To Java 3 11-29-2007 01:28 PM
Introduction to Hashtable JavaForums Java Blogs 1 11-20-2007 10:13 PM


All times are GMT +3. The time now is 08:00 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org