0

i have this weird problem: iterating over the keyset of a linkedhashmap, the get returns null on certain keys:

...

System.out.println("Map: "+wildCardSummaryKey);

try { 

    rowKeys = wildCardSummaryKey.keySet().iterator();

    while (rowKeys.hasNext()) {

       String rowKey = (String) rowKeys.next();

       ProcessTables.Counter counter = (ProcessTables.Counter) wildCardSummary.get(rowKey);

       System.out.println("key='"+rowKey+"', containsKey="+wildCardSummaryKey.containsKey(rowKey)+", value="+counter);

       System.out.println("count: "+counter.getCount());         
    }

} catch(NullPointerException e) {

    e.printStackTrace();
}

the output is (manually formatted for better readability):

Map: {
    abc:abc vpn:Klant:Stichting BlaBla College:Stichting BlaBla College=com.my.ProcessTables$Counter@54deac,
    abc:abc uni/nni:Klant:CA BlaBla F & O BV:CA BlaBla F & O BV=com.my.ProcessTables$Counter@43e6ee5f, 
    abc:abc vpn:Klant:CA BlaBla F & O BV:CA BlaBla F & O BV=com.my.ProcessTables$Counter@6e2bc1cc, 
    abc:abc uni/nni:Klant:Stichting BlaBla College:Stichting BlaBla College=com.my.ProcessTables$Counter@7ac07095
}

key='abc:abc vpn:Klant:Stichting BlaBla College:Stichting BlaBla College', containsKey=true, value=null

java.lang.NullPointerException
at com.my.ProcessTables.getData(ProcessTables.java:1166)
at com.my.ProcessTables.processRequest_getData(ProcessTables.java:1629)
at com.my.ProcessTables.processRequest(ProcessTables.java:81)

line 1166 is this line: System.out.println("count: "+counter.getCount());

so: although the iterator gives me the key i cannot get the value object from the map! what is going on? any ideas?

thanks! frank

frb
  • 53
  • 5

2 Answers2

3

The map which contains that key is wildCardSummaryKey as seen from this SOP statement.

System.out.println("key='"+rowKey+"', containsKey="+wildCardSummaryKey.containsKey(rowKey)+", value="+counter);

But the map from which you're trying to retrieve a value based on that rowKey is, wildCardSummary. This map probably doesn't contain the rowKey and thus returns a null which is assigned to the counter.

You probably need to get the counter from wildCardSummaryKey map.

ProcessTables.Counter counter = (ProcessTables.Counter) wildCardSummaryKey.get(rowKey);
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • ah, man, thanks! i have been looking at this for over a day now. you are absolutely right! – frb Apr 30 '14 at 09:15
1

LinkedHashMap may contain null as a legitimate value, as do several other Map implementations in Java.

If you look at this link, you can see that in the text:

http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html

"This class provides all of the optional Map operations, and permits null elements."

JimboK
  • 11
  • 1