File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes HashMap.get() not working properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap.get() not working properly" Watch "HashMap.get() not working properly" New topic
Author

HashMap.get() not working properly

Karen Castillo
Greenhorn

Joined: Jan 13, 2011
Posts: 10
Hi,

I have a HashMap: When I try to obtain the value for a given key it returns null.

The attrNameCap string is the same as the Pairs.getKey() string, but I still get null.

This is the code block:

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

Are you absolutely sure that key, with that capitalisation, is already in the Map? I'd suggest running a debugger (or sticking some debug code in) to look at what the actual keys in the Map are.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

A HashMap is case sensitive in its keys. If you want a case-insensitive Map use a TreeMap with a proper Comparator:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Karen Castillo
Greenhorn

Joined: Jan 13, 2011
Posts: 10
Added logs:




And the values are the same:



I have tried with both attrNameCap and attrName and I get a null value.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

Ah, well...

Firstly, you definitely need to be using attrName, because attrNameCap has different capitalisation from the key used in the Map.

Secondly, while attrName should work - the value stored in the Map against it is null! So in that case it would appear you are "successfully" pulling null from the Map.

The way you could tell the difference between a missing key and a key with a null value stored against it would be to use containsKey().
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

In your existing code on line 6 you use attrNameCap. Yet the listing of the contents of the map shows that the map contains a key with value "parentDomain". Use attrName instead of attrNameCap on line 6 and retry.

Also make sure that attrName does not contain any trailing spaces, and alter your code so that any trailing spaces around Strings become visible (eg. System.out.println("attrNameCap value: \"" + attrNameCap + "\"");)

Edit: Matthew is right, you have a null value there. I'll keep my previous suggestion here anyway, as it is a mistake I had trapped myself with a few times.
Karen Castillo
Greenhorn

Joined: Jan 13, 2011
Posts: 10
Hi,

Sorry about that. When I run my script the value on the profile is changed to null. I have added the values and tried again.


but it never goes inside:


Karen Castillo
Greenhorn

Joined: Jan 13, 2011
Posts: 10


the spacing is also correct.
Karen Castillo
Greenhorn

Joined: Jan 13, 2011
Posts: 10
Sorry for posting so much! If I add this code it works:



But it is not very clean.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Do I see an == sign after key?
Karen Castillo
Greenhorn

Joined: Jan 13, 2011
Posts: 10
fixed. Coding in a hurry and lacking skills..

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

I must admit, I think it's unlikely that the HashMap class simply isn't working correctly - it's too fundamental for basic bugs in it not to be widely known. But beyond what's already been suggested it's difficult to say definitively what's going wrong unless you can give us a standalone example that demonstrates the problem (a SSCCE), that we can run for ourselves.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
How many key-value pairs have you got in that map? Can you print out all the keys, and the key being tested, so you can see whether there is a spelling error, and whether the key being "got" matches the keys already in the Map?
Karen Castillo
Greenhorn

Joined: Jan 13, 2011
Posts: 10
This method imports profiles into Rhino ProfileTables. I will try to send a SSCCE, but I am a newbie and there are no guarantees. It is now working with the iterator, I will verify my code and see what I can do with a SSCCE.

Thanks for your help!
Karen Castillo
Greenhorn

Joined: Jan 13, 2011
Posts: 10
Campbell Ritchie wrote:How many key-value pairs have you got in that map? Can you print out all the keys, and the key being tested, so you can see whether there is a spelling error, and whether the key being "got" matches the keys already in the Map?




three pairs..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap.get() not working properly