{"record":{"id":"492770818e5dc915","repo":"stanfordnlp/CoreNLP","slug":"annotation-field-cannot-be-null","errorCode":null,"errorMessage":"Annotation field cannot be null","messagePattern":"Annotation field cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/SequenceMatchRules.java","lineNumber":1168,"sourceCode":"      if (this.group != 0) {\n        // Save context so value evaluation can happen\n        te.context = matched.toBasicSequenceMatchResult();\n      }\n      return te;\n    }\n  }\n\n\n  public static class CoreMapFunctionApplier<T,O> implements Function<CoreMap, O> {\n\n    final Env env;\n    final Class annotationField;\n    final Function<T,O> func;\n\n    public CoreMapFunctionApplier(Env env, Class annotationField, Function<T,O> func) {\n      this.annotationField = annotationField;\n      if (annotationField == null) {\n        throw new IllegalArgumentException(\"Annotation field cannot be null\");\n      }\n      this.func = func;\n      this.env = env;\n    }\n\n    @Override\n    public O apply(CoreMap cm) {\n      if (env != null) {\n        env.push(Expressions.VAR_SELF, cm);\n      }\n      try {\n        T field = (T) cm.get(annotationField);\n        return func.apply(field);\n      }  finally {\n        if (env != null) {\n          env.pop(Expressions.VAR_SELF);\n        }\n      }","sourceCodeStart":1150,"sourceCodeEnd":1186,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/SequenceMatchRules.java#L1150-L1186","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\nnew CoreMapFunctionApplier(env, null, func); // throws\n// after\nClass field = EnvLookup.lookupAnnotationKeyWithClassname(env, \"text\");\nnew CoreMapFunctionApplier(env, field, func);","handlingStrategy":"type-guard","validationCode":"if (annotationField == null) {\n  annotationField = CoreAnnotations.TextAnnotation.class; // sensible default\n}","typeGuard":"Class requireAnnotationKey(Class key) {\n  return key != null ? key : CoreAnnotations.TextAnnotation.class;\n}","tryCatchPattern":"try {\n  applier = new CoreMapFunctionApplier(env, annotationField, func);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"Annotation field cannot be null\")) {\n    applier = new CoreMapFunctionApplier(env, CoreAnnotations.TextAnnotation.class, func);\n  }\n}","preventionTips":["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"],"tags":["java","tokensregex","null-argument"],"backgroundTag":"null-argument","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}