{"record":{"id":"be17f02cc53d6449","repo":"stanfordnlp/CoreNLP","slug":"cannot-get-annotation-field-field","errorCode":null,"errorMessage":"Cannot get annotation field ${field}","messagePattern":"Cannot get annotation field (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/types/ValueFunctions.java","lineNumber":886,"sourceCode":"      if (in.get(0) == null ||\n              (!(in.get(0).get() instanceof CoreMap) && !(in.get(0).get() instanceof List))) {\n        return false;\n      }\n      if (in.get(1) == null ||\n              (!(in.get(1).get() instanceof Class) && !(in.get(1).get() instanceof String))) {\n        return false;\n      }\n      return true;\n    }\n    @Override\n    public Value apply(Env env, List<Value> in) {\n      Value cmv = in.get(0);\n      Object field = in.get(1).get();\n      Class annotationFieldClass = null;\n      if (field instanceof String)  {\n        annotationFieldClass = EnvLookup.lookupAnnotationKey(env, (String) field);\n        if (annotationFieldClass == null) {\n          throw new IllegalArgumentException(\"Cannot get annotation field \" + field);\n        }\n      } else if (field instanceof Class) {\n        annotationFieldClass = (Class)  field;\n      } else {\n        throw new IllegalArgumentException(\"Type mismatch on arg1: Cannot apply \" + this + \" to \" + in);\n      }\n      if (cmv.get() instanceof CoreMap) {\n        CoreMap cm = (CoreMap) cmv.get();\n        if (in.size() >= 3) {\n          Value v = in.get(2);\n          Object annotationObject = (v != null)? v.get():null;\n          cm.set(annotationFieldClass, annotationObject);\n        }\n        Object obj = cm.get(annotationFieldClass);\n        return Expressions.createValue(annotationFieldClass.getName(), obj);\n      } else if (cmv.get() instanceof List) {\n        List<CoreMap> cmList = (List<CoreMap>) cmv.get();\n        if (in.size() >= 3) {","sourceCodeStart":868,"sourceCodeEnd":904,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/types/ValueFunctions.java#L868-L904","documentation":"The annotation-value function (GET_ANNOTATION_VALUE style) resolves arg1 to a CoreMap annotation key class; when arg1 is a String that EnvLookup.lookupAnnotationKey cannot resolve to a registered annotation key class, this IllegalArgumentException is thrown. It means the named annotation field is unknown in the current environment.","triggerScenarios":"GET_ANNOTATION_VALUE(coremap, \"SomeField\") where \"SomeField\" is not a registered annotation key (no matching class in Env's annotation key mapping), case mismatches, or custom annotation keys never registered via env.","commonSituations":"Typo'd field names in TokensRegex rules, using custom Annotation classes without registering them in the environment, relying on fields from a different CoreNLP version/annotator that hasn't run.","solutions":["Correct the annotation field name string to a registered key (e.g. CoreAnnotations.TextAnnotation -> \"text\").","Register the custom annotation key in the Env (env.registerAnnotationKey or equivalent) before evaluating.","Pass the Class object directly as arg1 (field instanceof Class branch) instead of a String."],"exampleFix":"// before\nValue v = getFn.apply(env, Arrays.asList(cmValue, Expressions.createValue(\"Word\")));\n// after\nValue v = getFn.apply(env, Arrays.asList(cmValue, Expressions.createValue(CoreAnnotations.TextAnnotation.class)));","handlingStrategy":"validation","validationCode":"if (EnvLookup.lookupAnnotationKey(env, fieldName) == null) {\n    throw new IllegalArgumentException(\"Unknown annotation key: \" + fieldName);\n}","typeGuard":"boolean isResolvableField(Env env, Object field) {\n    return field instanceof Class || (field instanceof String && EnvLookup.lookupAnnotationKey(env, (String) field) != null);\n}","tryCatchPattern":"try {\n    v = getFn.apply(env, args);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Cannot get annotation field\")) {\n        v = null; // unknown field: treat as missing\n    } else throw e;\n}","preventionTips":["Register custom annotation keys in the Env before evaluation","Use canonical field names (e.g. 'word', 'tag', 'ner')","Prefer passing the Class object directly to avoid name resolution"],"tags":["tokensregex","annotation-key","corenlp"],"backgroundTag":"resource-not-found","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"}