{"record":{"id":"cba00b073d43fa74","repo":"stanfordnlp/CoreNLP","slug":"cannot-match-against-null-elements","errorCode":null,"errorMessage":"Cannot match against null elements","messagePattern":"Cannot match against null elements","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/SequenceMatcher.java","lineNumber":97,"sourceCode":"   */\n  public enum FindType { FIND_NONOVERLAPPING, FIND_ALL }\n  private FindType findType = FIND_NONOVERLAPPING;\n\n  // For FIND_ALL\n  private Iterator<Integer> curMatchIter = null;\n  private MatchedStates<T> curMatchStates = null;\n  private final Set<String> prevMatchedSignatures = new HashSet<>();\n\n  // Branching limit for searching with back tracking. Higher value makes the search faster but uses more memory.\n  private int branchLimit = 32;\n\n  protected SequenceMatcher(SequencePattern<T> pattern, List<? extends T> elements) {\n    this.pattern = pattern;\n    // NOTE: It is important elements DO NOT change as we do matches\n    // TODO: Should we just make a copy of the elements?\n    this.elements = elements;\n    if (elements == null) {\n      throw new IllegalArgumentException(\"Cannot match against null elements\");\n    }\n    this.regionEnd = elements.size();\n    this.priority = pattern.priority;\n    this.score = pattern.weight;\n    this.varGroupBindings = pattern.varGroupBindings;\n    matchedGroups = new MatchedGroup[pattern.totalGroups];\n  }\n\n  public void setBranchLimit(int blimit){\n    this.branchLimit = blimit;\n  }\n\n\n  /**\n   * Interface that specifies what to replace a matched pattern with.\n   *\n   * @param <T>\n   */","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/SequenceMatcher.java#L79-L115","documentation":"SequenceMatcher's constructor requires the element list to match against. Passing a null List makes matching impossible, so IllegalArgumentException 'Cannot match against null elements' is thrown immediately when constructing the matcher (e.g. via SequencePattern.getMatcher(pattern, null)).","triggerScenarios":"pattern.getMatcher(null); calling getMatcher with a variable that is null because an upstream tokenization/annotation step produced no list; matching against a null field fetched from a CoreMap.","commonSituations":"CoreMap annotation lookups returning null (missing tokens/words annotation) before matching; pipeline ordering mistakes where matching runs before annotation creation.","solutions":["Check the list for null before calling getMatcher and use Collections.emptyList() if matching should simply fail","Ensure the upstream annotator (e.g. tokenize, ssplit) has run so the annotation list exists","Guard CoreMap.get(...) results before matching"],"exampleFix":"// before\nTokenSequenceMatcher m = pattern.getMatcher(coreMap.get(CoreAnnotations.TokensAnnotation.class)); // may be null\n// after\nList<CoreLabel> tokens = coreMap.get(CoreAnnotations.TokensAnnotation.class);\nTokenSequenceMatcher m = pattern.getMatcher(tokens != null ? tokens : Collections.emptyList());","handlingStrategy":"type-guard","validationCode":"List<CoreLabel> tokens = coreMap.get(CoreAnnotations.TokensAnnotation.class);\nif (tokens == null) { /* skip matching or run tokenizer */ }","typeGuard":"boolean hasTokens(CoreMap cm) {\n  return cm != null && cm.get(CoreAnnotations.TokensAnnotation.class) != null;\n}","tryCatchPattern":"try {\n  matcher = pattern.getMatcher(elements);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"null elements\")) {\n    matcher = pattern.getMatcher(Collections.emptyList());\n  }\n}","preventionTips":["Ensure tokenizer/ssplit annotators run before TokensRegex matching","Null-check annotation lists before getMatcher","Pass Collections.emptyList() instead of null for degenerate inputs","Verify CoreMap.get() results are non-null"],"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"}