{"record":{"id":"549dac59ddaa7a3c","repo":"stanfordnlp/CoreNLP","slug":"invalid-node-pattern-variable-class-for-variable","errorCode":null,"errorMessage":"Invalid node pattern variable class:  for variable ","messagePattern":"Invalid node pattern variable class:  for variable ","errorType":"exception","errorClass":"java.lang.Error","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/Env.java","lineNumber":350,"sourceCode":"        if (seqPattern.getPatternExpr() instanceof SequencePattern.NodePatternExpr) {\n          return ((SequencePattern.NodePatternExpr) seqPattern.getPatternExpr()).nodePattern;\n        } else {\n          throw new Error(\"Invalid node pattern class: \" + seqPattern.getPatternExpr().getClass() + \" for variable \" + name);\n        }\n      } else if (obj instanceof SequencePattern.NodePatternExpr) {\n        SequencePattern.NodePatternExpr pe = (SequencePattern.NodePatternExpr) obj;\n        return pe.nodePattern;\n      } else if (obj instanceof NodePattern) {\n        return (NodePattern) obj;\n      } else if (obj instanceof String) {\n        try {\n          SequencePattern.NodePatternExpr pe = (SequencePattern.NodePatternExpr) parser.parseNode(this, (String) obj);\n          return pe.nodePattern;\n        } catch (Exception pex) {\n          throw new RuntimeException(\"Error parsing \" + obj + \" to node pattern\", pex);\n        }\n      } else {\n        throw new Error(\"Invalid node pattern variable class: \" + obj.getClass() + \" for variable \" + name);\n      }\n    }\n    return null;\n  }\n\n  public SequencePattern.PatternExpr getSequencePatternExpr(String name, boolean copy) {\n    Object obj = variables.get(name);\n    if (obj != null) {\n      if (obj instanceof SequencePattern) {\n        SequencePattern seqPattern = (SequencePattern) obj;\n        return seqPattern.getPatternExpr();\n      } else if (obj instanceof SequencePattern.PatternExpr) {\n        SequencePattern.PatternExpr pe = (SequencePattern.PatternExpr) obj;\n        return (copy)? pe.copy():pe;\n      } else if (obj instanceof NodePattern) {\n        return new SequencePattern.NodePatternExpr( (NodePattern) obj);\n      } else if (obj instanceof String) {\n        try {","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/Env.java#L332-L368","documentation":"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.","triggerScenarios":"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.","commonSituations":"Programmatic bindings with the wrong object type; variables intended for value/sequence use mistakenly referenced in node position.","solutions":["Bind a NodePattern, SequencePattern.NodePatternExpr, or a String parsable as a node pattern","Fix the rule to reference the variable in the position matching its type (value or sequence, not node)","Wrap plain values with an appropriate NodePattern (e.g. new StringAnnotationPattern(...)) before binding"],"exampleFix":"// before\nenv.bind(\"$n\", 42);\n// after\nenv.bind(\"$n\", new IntegerNodePattern(42));","handlingStrategy":"type-guard","validationCode":"Object obj = env.get(name);\nboolean ok = obj instanceof NodePattern || obj instanceof SequencePattern || obj instanceof SequencePattern.NodePatternExpr || obj instanceof String;\nif (!ok) throw new IllegalStateException(name + \" bound to unsupported class \" + obj.getClass());","typeGuard":"boolean bindableAsNode(Object o) { return o instanceof NodePattern || o instanceof SequencePattern.NodePatternExpr || (o instanceof SequencePattern) || (o instanceof String); }","tryCatchPattern":"try { env.getNodePattern(name); } catch (Error e) { log.error(\"Wrong type bound to {}: {}\", name, env.get(name).getClass()); throw e; }","preventionTips":["Type-check objects at bind time in helper methods","Do not bind raw values (numbers, lists) for pattern variables","Document variable types in rule files with comments"],"tags":["tokensregex","node-pattern","type-mismatch"],"backgroundTag":"type-mismatch","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"}