stanfordnlp/CoreNLP · error · RuntimeException

You cannot ask for the number of occurances of null.

Error message

You cannot ask for the number of occurances of null.

What it means

DirichletProcess.numOccurances(object) looks up how many times an object has been sampled, but a null object has no meaningful occurrence count in the underlying counter semantics used here. The method explicitly rejects null with RuntimeException rather than returning a count.

Solutions

  1. Check the object for null before calling and return 0 (a null object has never been sampled).
  2. Fix the upstream code that produced a null object reference (failed parse, missing map entry).
  3. Catch the RuntimeException at the call site only if null inputs are expected and can be treated as count 0.

Example fix

// before
double n = dp.numOccurances(token); // token may be null
// after
double n = (token == null) ? 0.0 : dp.numOccurances(token);
Defensive patterns

Strategy: type-guard

Validate before calling

if (object != null) {
  double n = dp.numOccurances(object);
}

Type guard

boolean isNonNull(Object o) { return o != null; }
// use: if (isNonNull(obj)) dp.numOccurances(obj);

Try / catch

double n;
try {
  n = dp.numOccurances(object);
} catch (RuntimeException e) {
  if (e.getMessage().contains("occurances of null")) n = 0.0; // null never sampled
  else throw e;
}

Prevention

When it happens

Trigger: Calling dp.numOccurances(null) directly, or passing a null that propagated from parsing/lookup code (e.g. a map miss returning null as the object).

Common situations: Querying statistics for a token that failed to parse or a key absent from an upstream map, so the variable holding the object is null.

Related errors


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

Appendix: source

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

  public DirichletProcess(ProbabilityDistribution<E> baseMeasure, double alpha) {
    this.baseMeasure = baseMeasure;
    this.alpha = alpha;
    this.sampled = new ClassicCounter<>();
    sampled.incrementCount(null, alpha);
  }

  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));

View on GitHub (pinned to 1b7edd19c4)