stanfordnlp/CoreNLP · error · java.lang.Error

Invalid node pattern variable class: for variable

Error message

Invalid node pattern variable class:  for variable 

What it means

Final type check in Env.getNodePattern: if the variable's bound object is neither a SequencePattern, NodePatternExpr, NodePattern, nor String, the library throws an Error stating the variable's class is not usable as a node pattern.

Solutions

  1. Bind a NodePattern, SequencePattern.NodePatternExpr, or a String parsable as a node pattern
  2. Fix the rule to reference the variable in the position matching its type (value or sequence, not node)
  3. Wrap plain values with an appropriate NodePattern (e.g. new StringAnnotationPattern(...)) before binding

Example fix

// before
env.bind("$n", 42);
// after
env.bind("$n", new IntegerNodePattern(42));
Defensive patterns

Strategy: type-guard

Validate before calling

Object obj = env.get(name);
boolean ok = obj instanceof NodePattern || obj instanceof SequencePattern || obj instanceof SequencePattern.NodePatternExpr || obj instanceof String;
if (!ok) throw new IllegalStateException(name + " bound to unsupported class " + obj.getClass());

Type guard

boolean bindableAsNode(Object o) { return o instanceof NodePattern || o instanceof SequencePattern.NodePatternExpr || (o instanceof SequencePattern) || (o instanceof String); }

Try / catch

try { env.getNodePattern(name); } catch (Error e) { log.error("Wrong type bound to {}: {}", name, env.get(name).getClass()); throw e; }

Prevention

When it happens

Trigger: Binding a variable to an unsupported type (List, Integer, Value, a compiled SequenceMatchResult, etc.) and then referencing it as a node pattern in a rule.

Common situations: Programmatic bindings with the wrong object type; variables intended for value/sequence use mistakenly referenced in node position.

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


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/549dac59ddaa7a3c. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/Env.java:350

        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;
  }

  public SequencePattern.PatternExpr getSequencePatternExpr(String name, boolean copy) {
    Object obj = variables.get(name);
    if (obj != null) {
      if (obj instanceof SequencePattern) {
        SequencePattern seqPattern = (SequencePattern) obj;
        return seqPattern.getPatternExpr();
      } else if (obj instanceof SequencePattern.PatternExpr) {
        SequencePattern.PatternExpr pe = (SequencePattern.PatternExpr) obj;
        return (copy)? pe.copy():pe;
      } else if (obj instanceof NodePattern) {
        return new SequencePattern.NodePatternExpr( (NodePattern) obj);
      } else if (obj instanceof String) {
        try {

View on GitHub (pinned to 1b7edd19c4)