stanfordnlp/CoreNLP · error · RuntimeException

You cannot ask for the probability of null.

Error message

You cannot ask for the probability of null.

What it means

DirichletProcess.probabilityOf(object) computes the probability of an object under the sampled base distribution, but null is not a valid random variable value, so the method throws RuntimeException for null input. It is also called by logProbabilityOf, so a null propagates there too.

Solutions

  1. Return a default probability (0.0, or log prob Double.NEGATIVE_INFINITY) for null before calling.
  2. Fix the upstream pipeline so scoring only receives non-null objects.
  3. Wrap scoring in a null check helper used by both probabilityOf and logProbabilityOf call sites.

Example fix

// before
double lp = dp.logProbabilityOf(token); // token may be null -> throws
// after
double lp = (token == null) ? Double.NEGATIVE_INFINITY : dp.logProbabilityOf(token);
Defensive patterns

Strategy: type-guard

Validate before calling

if (object != null) {
  double p = dp.probabilityOf(object);
}

Type guard

boolean isScorable(E o) { return o != null; }
// use: if (isScorable(token)) dp.logProbabilityOf(token); else NEGATIVE_INFINITY;

Try / catch

double lp;
try {
  lp = dp.logProbabilityOf(object);
} catch (RuntimeException e) {
  if (e.getMessage().contains("probability of null")) lp = Double.NEGATIVE_INFINITY;
  else throw e;
}

Prevention

When it happens

Trigger: Calling dp.probabilityOf(null) or dp.logProbabilityOf(null), typically with a null produced by a failed lookup or parse upstream.

Common situations: Scoring unknown/unparsed tokens where the token variable is null; scoring results of optional extraction code that returned null.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/stats/DirichletProcess.java:41

  public E drawSample(Random random) {
    E drawn = Counters.sample(sampled);
    if (drawn == null) {
      drawn = baseMeasure.drawSample(random);
    }
    sampled.incrementCount(drawn);
    return drawn;
  }

  public double numOccurances(E object) {
    if (object == null) {
      throw new RuntimeException("You cannot ask for the number of occurances of null.");
    }
    return sampled.getCount(object);
  }
  
  public double probabilityOf(E object) {
    if (object == null) {
      throw new RuntimeException("You cannot ask for the probability of null.");
    }

    if (sampled.keySet().contains(object)) {
      return sampled.getCount(object) / sampled.totalCount();
    } else {
      return 0.0;
    }
  }

  public double logProbabilityOf(E object) {
    return Math.log(probabilityOf(object));
  }

  public double probabilityOfNewObject() {
    return alpha / sampled.totalCount();
  }
  
}

View on GitHub (pinned to 1b7edd19c4)