stanfordnlp/CoreNLP · error · java.lang.Error
Invalid node pattern class: for variable
Error message
Invalid node pattern class: for variable
What it means
Env.getNodePattern resolves a variable to a NodePattern; if the bound object is a SequencePattern whose pattern expression is not a simple NodePatternExpr (e.g. a sequence or composite expression), it cannot be used as a node pattern and throws an Error (not Exception).
Solutions
- Bind the variable to a single node pattern (e.g. a NodePattern or NodePatternExpr), or use a literal node expression at the reference site
- If a sequence is intended, use it in sequence position with getSequencePatternExpr semantics, not as a node
- Split the sequence into separate node variables
Defensive patterns
Strategy: type-guard
Validate before calling
Object obj = env.get(name);
if (obj instanceof SequencePattern && !(((SequencePattern) obj).getPatternExpr() instanceof SequencePattern.NodePatternExpr)) {
throw new IllegalStateException(name + " is a sequence pattern; use it in sequence position, not as a node");
} Type guard
boolean isNodePatternUsable(Object o) { return o instanceof NodePattern || o instanceof SequencePattern.NodePatternExpr || (o instanceof SequencePattern s && s.getPatternExpr() instanceof SequencePattern.NodePatternExpr); } Try / catch
try { NodePattern np = env.getNodePattern(name); } catch (Error e) { log.error("Variable {} is not a node pattern: {}", name, e.getMessage()); throw e; } Prevention
- Use distinct variable naming conventions for node vs sequence variables
- Check the bound expression type when binding, not at lookup time
- Only reference single-token expressions in node position
When it happens
Trigger: Referencing, in node position (e.g. [$VAR] inside a rule node), a variable bound to a full sequence pattern rather than a single node pattern.
Common situations: Binding a variable to a multi-token sequence and then using it as a single token pattern in a rule; confusing sequence variables with node variables.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Invalid node pattern variable class: for variable
- Error parsing to node pattern
- Invalid sequence pattern variable class:
- Unexpected class in list while looking up word () in…
- Unexpected class while looking up
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/8622cdf18d4e60c8.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/tokensregex/Env.java:335
}
public void bind(String name, SequencePattern pattern) {
bind(name, pattern.getPatternExpr());
}
public void unbind(String name) {
bind(name, null);
}
public NodePattern getNodePattern(String name) {
Object obj = variables.get(name);
if (obj != null) {
if (obj instanceof SequencePattern) {
SequencePattern seqPattern = (SequencePattern) obj;
if (seqPattern.getPatternExpr() instanceof SequencePattern.NodePatternExpr) {
return ((SequencePattern.NodePatternExpr) seqPattern.getPatternExpr()).nodePattern;
} else {
throw new Error("Invalid node pattern class: " + seqPattern.getPatternExpr().getClass() + " for variable " + name);
}
} else if (obj instanceof SequencePattern.NodePatternExpr) {
SequencePattern.NodePatternExpr pe = (SequencePattern.NodePatternExpr) obj;
return pe.nodePattern;
} else if (obj instanceof NodePattern) {
return (NodePattern) obj;
} else if (obj instanceof String) {
try {
SequencePattern.NodePatternExpr pe = (SequencePattern.NodePatternExpr) parser.parseNode(this, (String) obj);
return pe.nodePattern;
} catch (Exception pex) {
throw new RuntimeException("Error parsing " + obj + " to node pattern", pex);
}
} else {
throw new Error("Invalid node pattern variable class: " + obj.getClass() + " for variable " + name);
}
}
return null;View on GitHub (pinned to 1b7edd19c4)