This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell 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


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Hash" Watch "Method Hash" New topic
Author

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:
[CODE]
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)iterator2.next( );
System.out.println( "Testing Method Return Set: " +entry.getKey( ) + " : " +entry.getValue( ) );
}//end while loop
/CODE]

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(Student.java:48)
at Student.main(Student.java:124)

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
Hi,

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.

HTH.


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:

HashMap.java:26: ';' 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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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!

Layne


Java API Documentation
The Java Tutorial
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Ernest,

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


C:\java>javac HashMap.java
HashMap.java:36: cannot resolve symbol
symbol : method put (java.lang.String,java.lang.String)
location: class HashMap
Student.studentDetails.put(key, value);
^
HashMap.java:37: cannot resolve symbol
symbol : method put (java.lang.String,java.lang.String)
location: class HashMap
return (String)Student.studentDetails.put(key, value);
^
HashMap.java:43: cannot resolve symbol
symbol : method keySet ()
location: class HashMap
Set keys = Student.studentDetails.keySet ( );
^
HashMap.java:52: cannot resolve symbol
symbol : method get (java.lang.String)
location: class HashMap
return(String)Student.studentDetails.get(value);
^
HashMap.java:58: 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

Maureen
^
Paul Sturrock
Bartender

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.

or:


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.
 
GeeCON Prague 2014
 
subject: Method Hash