stanfordnlp/CoreNLP · error · IllegalArgumentException
Annotation field cannot be null
Error message
Annotation field cannot be null
What it means
CoreMapFunctionApplier wraps a Function that extracts a value from a CoreMap via an annotation key class. Its constructor requires a non-null annotationField; passing null makes it impossible to know which annotation key to read, so IllegalArgumentException 'Annotation field cannot be null' is thrown at construction time.
Solutions
- Pass a valid Class annotation key, e.g. CoreAnnotations.TextAnnotation.class
- Resolve the field from env first and verify non-null before constructing the applier
- Fix the rule attributes so annotationField resolves instead of being null
Example fix
// before new CoreMapFunctionApplier(env, null, func); // throws // after Class field = EnvLookup.lookupAnnotationKeyWithClassname(env, "text"); new CoreMapFunctionApplier(env, field, func);
Defensive patterns
Strategy: type-guard
Validate before calling
if (annotationField == null) {
annotationField = CoreAnnotations.TextAnnotation.class; // sensible default
} Type guard
Class requireAnnotationKey(Class key) {
return key != null ? key : CoreAnnotations.TextAnnotation.class;
} Try / catch
try {
applier = new CoreMapFunctionApplier(env, annotationField, func);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("Annotation field cannot be null")) {
applier = new CoreMapFunctionApplier(env, CoreAnnotations.TextAnnotation.class, func);
}
} Prevention
- Resolve annotation keys via EnvLookup and assert non-null before constructing appliers
- Default to a standard key like TextAnnotation when lookup returns null
- Check that rule attributes/defaults resolve the annotation field
When it happens
Trigger: Constructing new CoreMapFunctionApplier(env, null, func) directly, or creating rules/extractors whose resultFunction setup passes a null annotation field (e.g. no annotationField resolved from env/attributes).
Common situations: Programmatic rule building where the annotation field lookup returned null (unregistered key name) and the null is then handed to the applier; misconfigured rule result.
Related errors
- Cannot match against null elements
- Must supply a target label to compute precision and recall…
- Invalid annotation key
- Unknown rule type:
- Error creating composite rule: no annotation field
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/492770818e5dc915.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/tokensregex/SequenceMatchRules.java:1168
if (this.group != 0) {
// Save context so value evaluation can happen
te.context = matched.toBasicSequenceMatchResult();
}
return te;
}
}
public static class CoreMapFunctionApplier<T,O> implements Function<CoreMap, O> {
final Env env;
final Class annotationField;
final Function<T,O> func;
public CoreMapFunctionApplier(Env env, Class annotationField, Function<T,O> func) {
this.annotationField = annotationField;
if (annotationField == null) {
throw new IllegalArgumentException("Annotation field cannot be null");
}
this.func = func;
this.env = env;
}
@Override
public O apply(CoreMap cm) {
if (env != null) {
env.push(Expressions.VAR_SELF, cm);
}
try {
T field = (T) cm.get(annotationField);
return func.apply(field);
} finally {
if (env != null) {
env.pop(Expressions.VAR_SELF);
}
}View on GitHub (pinned to 1b7edd19c4)