{"record":{"id":"6a24082cc4728b12","repo":"stanfordnlp/CoreNLP","slug":"unable-get-expression-for-variable","errorCode":null,"errorMessage":"Unable get expression for variable ","messagePattern":"Unable get expression for variable ","errorType":"exception","errorClass":"java.lang.RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/CoreMapExpressionExtractor.java","lineNumber":328,"sourceCode":"  /**\n   * Creates an extractor using the specified environment, and reading the rules from the given string\n   * @param env\n   * @param str\n   * @throws IOException, ParseException\n   */\n  public static CoreMapExpressionExtractor createExtractorFromString(Env env, String str) throws IOException, ParseException, TokenSequenceParseException {\n    TokenSequenceParser parser = new TokenSequenceParser();\n    CoreMapExpressionExtractor extractor = parser.getExpressionExtractor(env, new StringReader(str));\n    return extractor;\n  }\n\n  public Value getValue(String varname)\n  {\n    Expression expr = (Expression) env.get(varname);\n    if (expr != null) {\n      return expr.evaluate(env);\n    } else {\n      throw new RuntimeException(\"Unable get expression for variable \" + varname);\n    }\n  }\n\n  private List<CoreMap> extractCoreMapsToList(List<CoreMap> res, CoreMap annotation) {\n    List<T> exprs = extractExpressions(annotation);\n    for (T expr : exprs) {\n      res.add(expr.getAnnotation());\n    }\n    return res;\n  }\n\n  /**\n   * Returns list of coremaps that matches the specified rules.\n   *\n   * @param annotation\n   */\n  public List<CoreMap> extractCoreMaps(CoreMap annotation) {\n    List<CoreMap> res = new ArrayList<>();","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/CoreMapExpressionExtractor.java#L310-L346","documentation":"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.","triggerScenarios":"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.","commonSituations":"Typo in variable name, referencing a variable before the rules file that defines it is loaded, or missing environment bindings between separate extractor files.","solutions":["Define the variable in the rules file (variables: [ ... ] or env.bind(\"name\", expr)) before use","Fix typos between variable definition and lookup","Ensure all rules files that define variables are loaded into the same environment"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"// caller-side check\nExpression expr = (Expression) env.get(varname);\nif (expr == null) throw new IllegalStateException(\"Variable not defined in rules env: \" + varname);","typeGuard":"boolean variableIsDefined(CoreMapExpressionExtractor ex, Env env, String name) { return env.get(name) != null; }","tryCatchPattern":"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; }","preventionTips":["Define all variables in a shared variables block or defaults file loaded first","Keep variable names in constants to avoid typos","Log the set of bound variables (env) when a lookup fails"],"tags":["tokensregex","variables","undefined"],"backgroundTag":"missing-config-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}