stanfordnlp/CoreNLP · error · java.lang.RuntimeException
Invalid null annotation key
Error message
Invalid null annotation key
What it means
MatchedExpression.setAnnotations maps a list of values onto a parallel list of annotation keys (Classes). When it encounters a null entry in annotationKeys it cannot store the value, so it throws 'Invalid null annotation key'. A null key means the caller supplied a malformed key list.
Solutions
- Audit the annotationKeys list passed to setAnnotations and remove/replace null entries
- Ensure the key lookup (e.g. CoreAnnotations subclass constant) is not null before calling
- Guard with a null-check loop before invoking setAnnotations
Example fix
// before
annotationKeys.set(1, null);
expr.setAnnotations(...);
// after
if (annotationKeys.contains(null)) throw new IllegalStateException("null annotation key");
expr.setAnnotations(...); Defensive patterns
Strategy: validation
Validate before calling
if (annotationKeys == null || annotationKeys.stream().anyMatch(Objects::isNull))
throw new IllegalArgumentException("annotationKeys must not contain null"); Type guard
static boolean validKeys(List<Class<?>> keys) { return keys != null && keys.stream().allMatch(Objects::nonNull); } Try / catch
try { expr.setAnnotations(cm, values, annotationKeys, ...); } catch (RuntimeException e) { if (e.getMessage().contains("null annotation key")) { /* fix key list */ } else throw e; } Prevention
- Verify Class lookups for annotation keys never return null
- Use static CoreAnnotations class constants instead of reflection
- Assert key list non-null before annotate calls
When it happens
Trigger: Calling annotate/setAnnotations with an annotationKeys list that contains a null element while the value object is a List whose size covers that index.
Common situations: Programmatically building annotation key arrays where a Class lookup returned null (e.g. reflection miss or uninitialized key constant).
Related errors
- 2 arguments expected, got
- Annotation field cannot be null
- : Entry has multiple types for : . Taking type to be
- Attribute already defined:
- Attribute already defined
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/e4ec3e669a0d2428.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/tokensregex/MatchedExpression.java:84
public Function<MatchedExpression, Value> expressionToValue;
public Function<MatchedExpression,?> resultAnnotationExtractor;
public CoreMapAggregator tokensAggregator;
@Override
public Value apply(CoreMap in) {
return valueExtractor.apply(in);
}
private static void setAnnotations(CoreMap cm, List<Class> annotationKeys, Object obj) {
if (annotationKeys.size() > 1 && obj instanceof List) {
// List of annotationKeys, obj also list, we should try to match the objects to annotationKeys
List list = (List) obj;
int n = Math.min(list.size(), annotationKeys.size());
for (int i = 0; i < n; i++) {
Object v = list.get(i);
Class key = annotationKeys.get(i);
if (key == null) {
throw new RuntimeException("Invalid null annotation key");
}
if (v instanceof Value) {
cm.set(key, ((Value) v).get());
} else {
cm.set(key, v);
}
}
} else {
// Only a single object, set all annotationKeys to that obj
for (Class key:annotationKeys) {
if (key == null) {
throw new RuntimeException("Invalid null annotation key");
}
cm.set(key, obj);
}
}
}
View on GitHub (pinned to 1b7edd19c4)