Japid: Strange behavior when getting value of a key from HashMap

136 views Asked by At
<p> THAT'S OK: ${((Map<String,Integer>)view.get("upgrade_assignment_type_map")).toString()} </p>

<p> THAT'S ALSO OK:
for( String _name : ((Map<String, Integer>)view.get("upgrade_assignment_type_map")).keySet() ) {
<p> ${_name}: ${((Map<String, Integer>)view.get("upgrade_assignment_type_map")).get(_name)} </p>
}
</p>
<br><br>
<p> SADLY THAT'S NOT OK::: ${((Map<String, Integer>)view.get("upgrade_assignment_type_map")).get("MANUAL")} </p>

The above code segment prints following output which is surprising for me:

THAT'S OK: {AUTO_WITH_BALANCE_EMPHASIS=2, AUTO_WITH_OFFENSIVE_EMPHASIS=0, MANUAL=3, AUTO_WITH_DEFENSIVE_EMPHASIS=1}

THAT'S ALSO OK:

AUTO_WITH_BALANCE_EMPHASIS: 2

AUTO_WITH_OFFENSIVE_EMPHASIS: 0

MANUAL: 3

AUTO_WITH_DEFENSIVE_EMPHASIS: 1



SADLY THAT'S NOT OK:::

The last line of code segment tries to get the value of key "MANUAL" but it's not printing the value and if I try to convert it to toString (like ${((Map<String, Integer>)view.get("upgrade_assignment_type_map")).get("MANUAL").toString()} ) it gives me null at runtime. I don't understand why it's happening?

and when I print the length(after first colon <p> ${_name}: ${_name.length()} : ${((Map<String, Integer>)view.get("upgrade_assignment_type_map")).get(_name)} </p> ) of each key I got:

AUTO_WITH_BALANCE_EMPHASIS: 29 : 2

AUTO_WITH_OFFENSIVE_EMPHASIS: 29 : 0

MANUAL: 10 : 3

AUTO_WITH_DEFENSIVE_EMPHASIS: 30 : 1

Note: I am creating map inside java source as Map<String, Integer> nameToTypeMap = Maps.newHashMap();

2

There are 2 answers

0
gmuhammad On BEST ANSWER

Eclipse was not showing invalid characters and there were invalid characters (which were surprisingly compilable) in those string, so when I opened that particular file in vim, it shows me and I removed those invalid characters. That solves my problem.

9
Jon Skeet On

I suspect that the "MANUAL" entry has an unprintable character somewhere in it. Change one of your working lines like this:

<p> KEY_LENGTHS:
for( String _name : ((Map<String, Integer>)view.get("upgrade_assignment_type_map")).keySet() ) {
<p> ${_name}: ${_name.length()}</p>
</p>

If that shows MANUAL with a length other than 6, that suggests I'm right, and you should look at your data source to work out where the extra character is coming from.

If not, I would at least try to debug this outside the HTML part. I haven't used the Play framework, but presumably there must be some code populating this view... if you can break into a debugger there, it's likely to make it a lot easier to diagnose.

Another possibility is that the entry contains a non-ASCII character which looks like an ASCII character, but actually isn't. If you look at the source of the HTML, you may be able to see that (and possibly even the unprintable character, depending on how it's handled).