• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap.get() not working properly

 
Karen Castillo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A HashMap is case sensitive in its keys. If you want a case-insensitive Map use a TreeMap with a proper Comparator:
 
Karen Castillo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the spacing is also correct.
 
Karen Castillo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for posting so much! If I add this code it works:



But it is not very clean.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I see an == sign after key?
 
Karen Castillo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fixed. Coding in a hurry and lacking skills..

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic