Date
1 - 3 of 3
NullPointerException
Hi,
I'm trying to count occurrences of a specific token in text and output the total number of that token. I get the nullPointer, even though I excluded the cases of data not present in text. Here's what I have:
Imports: {
I'm trying to count occurrences of a specific token in text and output the total number of that token. I get the nullPointer, even though I excluded the cases of data not present in text. Here's what I have:
Imports: {
import static gate.Utils.*;
import java.util.*;
import java.util.stream.*;
import java.util.HashMap;
import java.util.Map;
}
Phase: DaysCount
Input: Days
Options: control = once
Rule: DCount
{Days}
-->
{
Map<String, Integer> countByStringMap = new TreeMap<String, Integer>();
for (Annotation annotation : inputAS.get("Days")) {
if(annotation !=null){
String string = (String) annotation.getFeatures().get("string");
Integer count = (Integer) countByStringMap.get(string);
if (count == null) {
countByStringMap.put(string, 1);
} else {
countByStringMap.put(string, count + 1);
}
}
for (Map.Entry entry : countByStringMap.entrySet()) {
System.out.println( entry.getKey() + "," + entry.getValue());
}
}
}
Ian Roberts
Where exactly does the NPE happen?
You’re iterating over all annotations of type “Days”, and asking each one for a feature that is also named “Days”. The most likely case I can see that would cause a NPE would be if a particular Days annotation does not have a Days feature at all (since a TreeMap without a Comparator does not permit null keys) - could that be the case in your data?
Ian
Hi,
I'm trying to count occurrences of a specific token in text and output the total number of that token. I get the nullPointer, even though I excluded the cases of data not present in text. Here's what I have:
Imports: {import static gate.Utils.*;import java.util.*;import java.util.stream.*;import java.util.HashMap;import java.util.Map;}Phase: DaysCountInput: DaysOptions: control = onceRule: DCount{Days}-->{Map<String, Integer> countByStringMap = new TreeMap<String, Integer>();for (Annotation annotation : inputAS.get("Days")) {if(annotation !=null){String string = (String) annotation.getFeatures().get("Days");Integer count = (Integer) countByStringMap.get(string);if (count == null) {countByStringMap.put(string, 1);} else {countByStringMap.put(string, count + 1);}}for (Map.Entry entry : countByStringMap.entrySet()) {System.out.println( entry.getKey() + "," + entry.getValue());}}}