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.
[ October 17, 2004: Message edited by: Barry Gaunt ]
Joined: Oct 04, 2004
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?
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.
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.
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.
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?
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 ]