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
- Return a default probability (0.0, or log prob Double.NEGATIVE_INFINITY) for null before calling.
- Fix the upstream pipeline so scoring only receives non-null objects.
- 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
- Guard both probabilityOf and logProbabilityOf call sites with null checks.
- Assign null objects a probability of 0 (log: -infinity) by convention.
- Ensure tokenization/extraction code never produces null objects for scoring.
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
- You cannot ask for the number of occurances of null.
- no negative parameters allowed!
- Parameters must be non-negative!
- Parameters must have positive mass!
- total mass must be positive!
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)