stanfordnlp/CoreNLP · error · NullPointerException

Node name " + name + " does not exist in the searched tree

Error message

Node name " + name + " does not exist in the searched tree

What it means

RelabelNode.evaluate supports relabel patterns like '/regex/{...=name...}' that reference captured nodes by name. When the relabel template references a name that the tregex match did not bind to any node, tregex.getNode returns null and a NullPointerException is thrown to signal the missing named node.

Solutions

  1. Ensure every name referenced in the relabel pattern appears as a named capture ('=name') in the accompanying tregex pattern.
  2. Check spelling — names are matched exactly.
  3. Wrap Tsurgeon processing in try/catch (RuntimeException) to report the bad script entry instead of aborting the whole batch.

Example fix

// before
tregex: "@NP" relabel: "/^NP$/{=head}" // '=head' never captured
// after
tregex: "@NP <1=head" relabel: "/^NP$/{=head}"
Defensive patterns

Strategy: validation

Validate before calling

static boolean relabelNamesCaptured(String tregex, String relabelTemplate) {
  java.util.regex.Matcher m = java.util.regex.Pattern.compile("\\{=([A-Za-z0-9_]+)\\}").matcher(relabelTemplate);
  while (m.find()) {
    if (!tregex.contains("=" + m.group(1))) return false;
  }
  return true;
}

Try / catch

try {
  tree = op.evaluate(tree, matcher);
} catch (NullPointerException e) {
  if (e.getMessage() != null && e.getMessage().startsWith("Node name ")) {
    log.warn("Relabel references unknown node: " + e.getMessage());
  } else throw e;
}

Prevention

When it happens

Trigger: A relabel operation with a '{=name}' or '/{name}/' reference where `name` never appears as a captured node name (no '=name' in the tregex pattern), so tregex.getVariableString/getNode finds nothing.

Common situations: Mismatch between the names used in the relabel template and those captured in the tregex expression (typo, renamed variable, pattern changed but relabel not updated); running Tsurgeon scripts where tregex and relabel were edited independently.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/trees/tregex/tsurgeon/RelabelNode.java:161

    public Tree evaluate(Tree tree, TregexMatcher tregex) {
      Tree nodeToRelabel = childMatcher[0].evaluate(tree, tregex);
      switch (mode) {
      case FIXED: {
        nodeToRelabel.label().setValue(newLabel);
        break;
      }
      case REGEX: {
        Matcher m = labelRegex.matcher(nodeToRelabel.label().value());
        StringBuilder label = new StringBuilder();
        for (String chunk : replacementPieces) {
          if (variablePattern.matcher(chunk).matches()) {
            String name = chunk.substring(2, chunk.length() - 1);
            label.append(Matcher.quoteReplacement(tregex.getVariableString(name)));
          } else if (nodePattern.matcher(chunk).matches()) {
            String name = chunk.substring(2, chunk.length() - 1);
            Tree node = tregex.getNode(name);
            if (node == null) {
              throw new NullPointerException("Node name " + name + " does not exist in the searched tree");
            }
            label.append(Matcher.quoteReplacement(node.value()));
          } else {
            label.append(chunk);
          }
        }
        nodeToRelabel.label().setValue(m.replaceAll(label.toString()));
        break;
      }
      default:
        throw new AssertionError("Unsupported relabel mode " + mode);
      }
      return tree;
    }
  }

  @Override
  public String toString() {

View on GitHub (pinned to 1b7edd19c4)