stanfordnlp/CoreNLP · error · IllegalArgumentException

ERROR: null count for item item!

Error message

ERROR: null count for item item!

What it means

validateCounter, the pre-check helper for simpleGoodTuring, iterates every counter entry and requires a non-null count value. A null Double count cannot participate in smoothing arithmetic, so an IllegalArgumentException naming the offending item is thrown. In practice Counter implementations usually box counts as double and never store null, so this typically indicates a corrupted or hand-constructed counter.

Solutions

  1. Sanitize the counter before calling: replace null counts with 0.0 or remove those entries
  2. Check the code path that built the counter and ensure it never inserts null values
  3. Use Counters utilities or a wrapper that rejects/normalizes null counts

Example fix

// before
Counter<String> c = buildCounterFromMap(rawMap); // may contain nulls
// after
for (Map.Entry<String, Double> e : rawMap.entrySet()) {
  if (e.getValue() != null) c.setCount(e.getKey(), e.getValue());
}
Distribution<String> d = Distribution.simpleGoodTuring(c, totalKeys);
Defensive patterns

Strategy: validation

Validate before calling

for (Map.Entry<E, Double> e : counter.entrySet()) {
  if (e.getValue() == null) throw new IllegalStateException("null count for " + e.getKey());
}

Try / catch

try {
  Distribution<E> d = Distribution.simpleGoodTuring(counter, n);
} catch (IllegalArgumentException e) {
  if (e.getMessage().contains("null count")) { /* sanitize counter and retry */ }
}

Prevention

When it happens

Trigger: Passing a Counter<E> to Distribution.simpleGoodTuring() where some entry's value is null (e.g. a map-backed counter built via put(item, null) or deserialized from data with missing values).

Common situations: Counters assembled manually from Maps or loaded from external files/JSON where a count field is absent and mapped to null instead of 0.0.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/stats/Distribution.java:490

    dist.counter = new ClassicCounter<>();
    for (Map.Entry<E, Double> entry : counter.entrySet()) {
      E item = entry.getKey();
      Integer count = (int) Math.round(entry.getValue());
      dist.counter.setCount(item, probsByCount.getCount(count));
    }
    dist.numberOfKeys = numberOfKeys;
    dist.reservedMass = sgt.getProbabilityForUnseen();
    return dist;

  }

  /* Helper to simpleGoodTuringSmoothedCounter() */
  private static <E> void validateCounter(Counter<E> counts) {
    for (Map.Entry<E, Double> entry : counts.entrySet()) {
      E item = entry.getKey();
      Double dblCount = entry.getValue();
      if (dblCount == null) {
        throw new IllegalArgumentException("ERROR: null count for item " + item + "!");
      }
      if (dblCount < 0) {
        throw new IllegalArgumentException("ERROR: negative count " + dblCount + " for item " + item + "!");
      }
    }
  }

  /* Helper to simpleGoodTuringSmoothedCounter() */
  private static <E> Counter<Integer> collectCountCounts(Counter<E> counts) {
    Counter<Integer> cc = new ClassicCounter<>(); // counts of counts
    for (Map.Entry<E, Double> entry : counts.entrySet()) {
      //E item = entry.getKey();
      Integer count = (int) Math.round(entry.getValue());
      cc.incrementCount(count);
    }
    return cc;
  }

View on GitHub (pinned to 1b7edd19c4)