{"record":{"id":"e4ec3e669a0d2428","repo":"stanfordnlp/CoreNLP","slug":"invalid-null-annotation-key","errorCode":null,"errorMessage":"Invalid null annotation key","messagePattern":"Invalid null annotation key","errorType":"exception","errorClass":"java.lang.RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/MatchedExpression.java","lineNumber":84,"sourceCode":"    public Function<MatchedExpression, Value> expressionToValue;\n    public Function<MatchedExpression,?> resultAnnotationExtractor;\n    public CoreMapAggregator tokensAggregator;\n\n    @Override\n    public Value apply(CoreMap in) {\n      return valueExtractor.apply(in);\n    }\n\n    private static void setAnnotations(CoreMap cm, List<Class> annotationKeys, Object obj) {\n      if (annotationKeys.size() > 1 && obj instanceof List) {\n        // List of annotationKeys, obj also list, we should try to match the objects to annotationKeys\n        List list = (List) obj;\n        int n = Math.min(list.size(), annotationKeys.size());\n        for (int i = 0; i < n; i++) {\n          Object v = list.get(i);\n          Class key = annotationKeys.get(i);\n          if (key == null) {\n            throw new RuntimeException(\"Invalid null annotation key\");\n          }\n          if (v instanceof Value) {\n            cm.set(key, ((Value) v).get());\n          } else {\n            cm.set(key, v);\n          }\n        }\n      } else {\n        // Only a single object, set all annotationKeys to that obj\n        for (Class key:annotationKeys) {\n          if (key == null) {\n            throw new RuntimeException(\"Invalid null annotation key\");\n          }\n          cm.set(key, obj);\n        }\n      }\n    }\n","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/MatchedExpression.java#L66-L102","documentation":"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.","triggerScenarios":"Calling annotate/setAnnotations with an annotationKeys list that contains a null element while the value object is a List whose size covers that index.","commonSituations":"Programmatically building annotation key arrays where a Class lookup returned null (e.g. reflection miss or uninitialized key constant).","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"],"exampleFix":"// before\nannotationKeys.set(1, null);\nexpr.setAnnotations(...);\n// after\nif (annotationKeys.contains(null)) throw new IllegalStateException(\"null annotation key\");\nexpr.setAnnotations(...);","handlingStrategy":"validation","validationCode":"if (annotationKeys == null || annotationKeys.stream().anyMatch(Objects::isNull))\n    throw new IllegalArgumentException(\"annotationKeys must not contain null\");","typeGuard":"static boolean validKeys(List<Class<?>> keys) { return keys != null && keys.stream().allMatch(Objects::nonNull); }","tryCatchPattern":"try { expr.setAnnotations(cm, values, annotationKeys, ...); } catch (RuntimeException e) { if (e.getMessage().contains(\"null annotation key\")) { /* fix key list */ } else throw e; }","preventionTips":["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"],"tags":["tokensregex","null-pointer","annotations"],"backgroundTag":"null-argument","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}