stanfordnlp/CoreNLP · error · IllegalArgumentException

governor or dependent cannot be null

Error message

governor or dependent cannot be null

What it means

The UnnamedDependency(String, String) constructor rejects null regent or dependent words, since a dependency edge must reference two actual tokens. Either argument null triggers IllegalArgumentException at construction time.

Solutions

  1. Check both strings for null before constructing and skip or log the edge
  2. Substitute a placeholder like "ROOT" when a null head is semantically valid
  3. Fix the upstream extraction so missing tokens are filtered out earlier

Example fix

// before
edges.add(new UnnamedDependency(headStr, depStr));
// after
if (headStr != null && depStr != null) {
  edges.add(new UnnamedDependency(headStr, depStr));
}
Defensive patterns

Strategy: validation

Validate before calling

if (regent == null || dependent == null) {
  throw new IllegalArgumentException("cannot build dependency from null token");
}
new UnnamedDependency(regent, dependent);

Type guard

boolean isBuildable(String regent, String dependent) {
  return regent != null && dependent != null && !regent.isEmpty() && !dependent.isEmpty();
}

Try / catch

try {
  edges.add(new UnnamedDependency(regentStr, dependentStr));
} catch (IllegalArgumentException e) {
  logger.debug("skipping null-token edge");
}

Prevention

When it happens

Trigger: new UnnamedDependency(null, dependentString) or new UnnamedDependency(regentString, null), typically when regent/dependent come from nullable lookups (parser output, map lookups, optional tokens).

Common situations: Building dependency edges from data with missing head or child tokens (e.g. ROOT pseudo-tokens not handled, empty extractions from relation extraction or dependency conversion).

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/trees/UnnamedDependency.java:38

  private static final long serialVersionUID = -3768440215342256085L;

  // We store the text of the labels separately because it looks like
  // it is possible for an object to request a hash code using itself
  // in a partially reconstructed state when unserializing.  For
  // example, a TreeGraphNode might ask for the hash code of an
  // UnnamedDependency, which then uses an unfilled member of the same
  // TreeGraphNode to get the hash code.  Keeping the text of the
  // labels breaks that possible cycle.
  protected final String regentText;
  protected final String dependentText;

  private final Label regent;
  private final Label dependent;

  public UnnamedDependency(String regent, String dependent) {
    if (regent == null || dependent == null) {
      throw new IllegalArgumentException("governor or dependent cannot be null");
    }
    
    CoreLabel headLabel = new CoreLabel();
    headLabel.setValue(regent);
    headLabel.setWord(regent);
    this.regent = headLabel;
    
    CoreLabel depLabel = new CoreLabel();
    depLabel.setValue(dependent);
    depLabel.setWord(dependent);
    this.dependent = depLabel;

    regentText = regent;
    dependentText = dependent;
  }

  public UnnamedDependency(Label regent, Label dependent) {
    if (regent == null || dependent == null) {

View on GitHub (pinned to 1b7edd19c4)