{"record":{"id":"ebcd94f43a8b4e89","repo":"stanfordnlp/CoreNLP","slug":"error-null-count-for-item-item","errorCode":null,"errorMessage":"ERROR: null count for item item!","messagePattern":"ERROR: null count for item item!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/Distribution.java","lineNumber":490,"sourceCode":"    dist.counter = new ClassicCounter<>();\n    for (Map.Entry<E, Double> entry : counter.entrySet()) {\n      E item = entry.getKey();\n      Integer count = (int) Math.round(entry.getValue());\n      dist.counter.setCount(item, probsByCount.getCount(count));\n    }\n    dist.numberOfKeys = numberOfKeys;\n    dist.reservedMass = sgt.getProbabilityForUnseen();\n    return dist;\n\n  }\n\n  /* Helper to simpleGoodTuringSmoothedCounter() */\n  private static <E> void validateCounter(Counter<E> counts) {\n    for (Map.Entry<E, Double> entry : counts.entrySet()) {\n      E item = entry.getKey();\n      Double dblCount = entry.getValue();\n      if (dblCount == null) {\n        throw new IllegalArgumentException(\"ERROR: null count for item \" + item + \"!\");\n      }\n      if (dblCount < 0) {\n        throw new IllegalArgumentException(\"ERROR: negative count \" + dblCount + \" for item \" + item + \"!\");\n      }\n    }\n  }\n\n  /* Helper to simpleGoodTuringSmoothedCounter() */\n  private static <E> Counter<Integer> collectCountCounts(Counter<E> counts) {\n    Counter<Integer> cc = new ClassicCounter<>(); // counts of counts\n    for (Map.Entry<E, Double> entry : counts.entrySet()) {\n      //E item = entry.getKey();\n      Integer count = (int) Math.round(entry.getValue());\n      cc.incrementCount(count);\n    }\n    return cc;\n  }\n","sourceCodeStart":472,"sourceCodeEnd":508,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/Distribution.java#L472-L508","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Sanitize the counter before calling: replace null counts with 0.0 or remove those entries","Check the code path that built the counter and ensure it never inserts null values","Use Counters utilities or a wrapper that rejects/normalizes null counts"],"exampleFix":"// before\nCounter<String> c = buildCounterFromMap(rawMap); // may contain nulls\n// after\nfor (Map.Entry<String, Double> e : rawMap.entrySet()) {\n  if (e.getValue() != null) c.setCount(e.getKey(), e.getValue());\n}\nDistribution<String> d = Distribution.simpleGoodTuring(c, totalKeys);","handlingStrategy":"validation","validationCode":"for (Map.Entry<E, Double> e : counter.entrySet()) {\n  if (e.getValue() == null) throw new IllegalStateException(\"null count for \" + e.getKey());\n}","typeGuard":null,"tryCatchPattern":"try {\n  Distribution<E> d = Distribution.simpleGoodTuring(counter, n);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"null count\")) { /* sanitize counter and retry */ }\n}","preventionTips":["Never insert null counts into Counters; use 0.0","Sanitize deserialized counters before smoothing","Prefer primitive-double count accessors (getCount) over boxed values when building counters"],"tags":["java","null","counter-validation","statistics"],"backgroundTag":"null-argument","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}