This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Method Hash Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Hash" Watch "Method Hash" New topic

Method Hash

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Is anyone able to tell me how I check what is being stored in a HashMap?

The code I have is:

I was unsuccessful with putting a System.out.println.

I have also tried:
public static void ReturnSet ( )
Set entries = Student.StudentDetails.entrySet( );
Iterator iterator2 = entries.iterator( );
System.out.println("The map contains the following associations: ");
while (iterator2.hasNext( ) )
Map.Entry entry = (Map.Entry) );
System.out.println( "Testing Method Return Set: " +entry.getKey( ) + " : " +entry.getValue( ) );
}//end while loop

But the while loop is not even being executed so I am assuming nothing is being stored in the addEntry method. Hence the reason why I am tryng to see what is going on but not having much luck.

Any help or pointers into the right direction would be appreciated.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Why do you put double quotes around your parameters to the put method?
You will always be putting the same pair of values in your HashMap. So if that method is called you would have exactly one entry in your HashMap.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The following code runs as expected:

[ October 17, 2004: Message edited by: Barry Gaunt ]
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Barry, Many thanks for your help. I'm pulling my hair out with these HashMaps.

When looking at your code, and then looking at my code the only difference I can see is that I have used a constructor to initialise my HashMap and a method to print out what is in the HashMap.

I took the quotations off the addEntry and now get the following error message:
Exception in thread "main" java.lang.NullPointerException
at Student.addEntry(
at Student.main(

My understanding is this is telling me that like an array, which starts from 0, the size is not big enough. But I don't understand as I have initialised my hashmap at the beginning and in the constructor?

My code is as follows:
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884

Zooming in straight to the offending statement:

You're attempting to access a static HashMap StudentDetails & invoking its method put().

And you've put the codes that instantiate this HashMap inside your constructor.

The NullPointerException usually indicates that the object you're using is null. In this case, your StudentDetails had not been instantiated at all. You didn't call the constructor in your main(). Since it is null, attempting to call any of its (HashMap's) method(s) will definitely cause a NullPointerException.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Many thanks for your help. At last I eventually got there.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Thought I'd got there................ But obviously NOT.

The error message I'm getting now is: ';' expected
public Static HashMap studentDetails = new HashMap( );

I don't understand as this is the same as all my examples, including what you have written above.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24193

You've capitalized "Static" on the offending line; it has to be lowercase.

Here's a good rule to follow that will help you avoid problems like this: always name your ClassesLikeThis, and your variablesLikeThis (upper case classes, lower case variables.) The only things that are in mixed case and start with a capital letter should be class names. All keywords are all lower-case.

[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Also, it is typically helpful to point out which line is causing the error. From the compiler's message, I see it is line 26, but I hate counting that high. Besides, I can't be sure that you've posted every line from the offending source file.

Keep coding!


Java API Documentation
The Java Tutorial
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218

I still get the same error messages when I ensure that static is lower case.

C:\java>javac cannot resolve symbol
symbol : method put (java.lang.String,java.lang.String)
location: class HashMap
Student.studentDetails.put(key, value);
^ cannot resolve symbol
symbol : method put (java.lang.String,java.lang.String)
location: class HashMap
return (String)Student.studentDetails.put(key, value);
^ cannot resolve symbol
symbol : method keySet ()
location: class HashMap
Set keys = Student.studentDetails.keySet ( );
^ cannot resolve symbol
symbol : method get (java.lang.String)
location: class HashMap
^ cannot resolve symbol
symbol : method entrySet ()
location: class HashMap
Set entries = studentDetails.entrySet( );

My understanding of this is it is not seeing any of my methods in a different class called Student. Why Not?

Is there any where that you can suggest I look for an example of code that demonstrates how to manipulate calling classes outside the class where you want to call it from - it would be good if this was using a HashMap?

Thanking you in advance

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Your program is a little confused. There is a class supplied with the JDK: java.util.HashMap. But in the context of your program there is another, different HashMap definition - your class HashMap. In your program when you write "new HashMap()" which one are you using? The way the compiler sees it is you are using the one it has visibility of. Unless you have imported java.util.HashMap into the class which includes the code "new HashMap()" (and assuming all your classes are in the same package), then "new HashMap()" is a call to create a new instance of your class, not Sun's. Since your HashMap class doesn't have a method put(Object key, Object value) that is whay the compiler is telling you that it "cannot resolve symbol".

What happens if you import java.util.HashMap into your class and write "new HashMap()"? Well the copiler now has a problem, you have defined a class called HashMap which it knows about and imported a class called HashMap, which it now also knows about. How sdoes it decide which to use? In that case you'll get a compilation error, unless you use the HashMap with its complete package namer, e.g.


Either way, your problem comes because you have defined a class which has the same name as an existing one, which is not a good thing to do. Try renaming your class.

Another tip for the future, when you are designing your classes, consider what your class defines or does when you are naming it. A HashMap is a collection of objects, your class is not. You have the comment

//This is the main program to get input using Java's JOptionPane class and call the showInputDialog method

which describes what it does, so calling your class something as simple as "MainProgram" would do.
[ October 21, 2004: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218

AT LAST!!! I finally got there.

Many thanks to you all (Barry Gaunt, Cheng Wei Lee, Ernest Friedman-Hill, Layne Lund and Paul Sturrock).

My HashMap is working just.........perfectly.

I guess I learnt the hard way though about nameing my public classes with care. Saved the file as MainProgHash - and it all went like a dream.

THANK YOU ALL SO MUCH for the time and effort you have given me. It really is appreciated. I hope one day that I will get good and help other people like you have helped me.
I agree. Here's the link:
subject: Method Hash
It's not a secret anymore!