stanfordnlp/CoreNLP · error · java.lang.RuntimeException

Unable get expression for variable

Error message

Unable get expression for variable 

What it means

CoreMapExpressionExtractor.getValue looks up a variable in the environment and expects it to hold an Expression; if env.get(varname) returns null (variable was never defined), it throws a RuntimeException indicating the variable has no expression.

Solutions

  1. Define the variable in the rules file (variables: [ ... ] or env.bind("name", expr)) before use
  2. Fix typos between variable definition and lookup
  3. Ensure all rules files that define variables are loaded into the same environment
Defensive patterns

Strategy: type-guard

Validate before calling

// caller-side check
Expression expr = (Expression) env.get(varname);
if (expr == null) throw new IllegalStateException("Variable not defined in rules env: " + varname);

Type guard

boolean variableIsDefined(CoreMapExpressionExtractor ex, Env env, String name) { return env.get(name) != null; }

Try / catch

try { Value v = extractor.getValue(name); } catch (RuntimeException e) { if (e.getMessage().startsWith("Unable get expression")) { log.error("Undefined TokensRegex variable: {}", name); return Value.nullValue(); } throw e; }

Prevention

When it happens

Trigger: Calling extractor.getValue("name") (or evaluating a rule referencing $name) where the variable was never declared in the rules file via a 'variables' block, env.bind, or defaults.

Common situations: Typo in variable name, referencing a variable before the rules file that defines it is loaded, or missing environment bindings between separate extractor files.

Understand the failure class

Background: "missing required config value" errors: why libraries refuse to start when a configuration key is empty, unset, or blank — this error's family across 48 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/CoreMapExpressionExtractor.java:328

  /**
   * Creates an extractor using the specified environment, and reading the rules from the given string
   * @param env
   * @param str
   * @throws IOException, ParseException
   */
  public static CoreMapExpressionExtractor createExtractorFromString(Env env, String str) throws IOException, ParseException, TokenSequenceParseException {
    TokenSequenceParser parser = new TokenSequenceParser();
    CoreMapExpressionExtractor extractor = parser.getExpressionExtractor(env, new StringReader(str));
    return extractor;
  }

  public Value getValue(String varname)
  {
    Expression expr = (Expression) env.get(varname);
    if (expr != null) {
      return expr.evaluate(env);
    } else {
      throw new RuntimeException("Unable get expression for variable " + varname);
    }
  }

  private List<CoreMap> extractCoreMapsToList(List<CoreMap> res, CoreMap annotation) {
    List<T> exprs = extractExpressions(annotation);
    for (T expr : exprs) {
      res.add(expr.getAnnotation());
    }
    return res;
  }

  /**
   * Returns list of coremaps that matches the specified rules.
   *
   * @param annotation
   */
  public List<CoreMap> extractCoreMaps(CoreMap annotation) {
    List<CoreMap> res = new ArrayList<>();

View on GitHub (pinned to 1b7edd19c4)