stanfordnlp/CoreNLP · error · IllegalArgumentException
Cannot match against null elements
Error message
Cannot match against null elements
What it means
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)).
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
Example fix
// before TokenSequenceMatcher m = pattern.getMatcher(coreMap.get(CoreAnnotations.TokensAnnotation.class)); // may be null // after List<CoreLabel> tokens = coreMap.get(CoreAnnotations.TokensAnnotation.class); TokenSequenceMatcher m = pattern.getMatcher(tokens != null ? tokens : Collections.emptyList());
Defensive patterns
Strategy: type-guard
Validate before calling
List<CoreLabel> tokens = coreMap.get(CoreAnnotations.TokensAnnotation.class);
if (tokens == null) { /* skip matching or run tokenizer */ } Type guard
boolean hasTokens(CoreMap cm) {
return cm != null && cm.get(CoreAnnotations.TokensAnnotation.class) != null;
} Try / catch
try {
matcher = pattern.getMatcher(elements);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("null elements")) {
matcher = pattern.getMatcher(Collections.emptyList());
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: CoreMap annotation lookups returning null (missing tokens/words annotation) before matching; pipeline ordering mistakes where matching runs before annotation creation.
Related errors
- Annotation field cannot be null
- 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/cba00b073d43fa74.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/tokensregex/SequenceMatcher.java:97
*/
public enum FindType { FIND_NONOVERLAPPING, FIND_ALL }
private FindType findType = FIND_NONOVERLAPPING;
// For FIND_ALL
private Iterator<Integer> curMatchIter = null;
private MatchedStates<T> curMatchStates = null;
private final Set<String> prevMatchedSignatures = new HashSet<>();
// Branching limit for searching with back tracking. Higher value makes the search faster but uses more memory.
private int branchLimit = 32;
protected SequenceMatcher(SequencePattern<T> pattern, List<? extends T> elements) {
this.pattern = pattern;
// NOTE: It is important elements DO NOT change as we do matches
// TODO: Should we just make a copy of the elements?
this.elements = elements;
if (elements == null) {
throw new IllegalArgumentException("Cannot match against null elements");
}
this.regionEnd = elements.size();
this.priority = pattern.priority;
this.score = pattern.weight;
this.varGroupBindings = pattern.varGroupBindings;
matchedGroups = new MatchedGroup[pattern.totalGroups];
}
public void setBranchLimit(int blimit){
this.branchLimit = blimit;
}
/**
* Interface that specifies what to replace a matched pattern with.
*
* @param <T>
*/View on GitHub (pinned to 1b7edd19c4)