This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes why am i getting a null value on fetching from map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why am i getting a null value on fetching from map" Watch "why am i getting a null value on fetching from map" New topic
Author

why am i getting a null value on fetching from map

utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
I have written the following code which puts in three key value pairs in a map and then fetches one of them based on the key.



I am getting a null value while doing the get operation why??
I know that I have overridden the equals and hashcode functions of only the key class and not the value class. But it should still work because on doing the get operation only key class is called is that no correct?



[HENRY: Added code tags]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40


Its the hashCode() method -- not the hashcode() method -- that you need to override.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

I suggest you use the @Override annotation. That would have informed you about the typo in hashCode.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
Hi

Thanks Henry and Rob so much.
I would have never found that out. Can you please tell something more about @Override . I have never come across this before.
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

utsav gupta wrote:Can you please tell something more about @Override . I have never come across this before.

From the doc,
Indicates that a method declaration is intended to override a method declaration in a supertype. If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:

* The method does override or implement a method declared in a supertype.
* The method has a signature that is override-equivalent to that of any public method declared in Object.


Thus apart from the advantage that Rob already mentioned, the other benefit you get while using this annotation is that if the parent class modified, the compiler will make sure that the child classes have been updated accordingly.


http://muhammadkhojaye.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why am i getting a null value on fetching from map
 
Similar Threads
Hash Map question ...
Hashcode for HashMap not dependent on the hashcode of key added?
is it worth adding a class to distinguish types
Custom keys in tree map
Help on Hashcode