{"record":{"id":"7e53e1cc5325e722","repo":"stanfordnlp/CoreNLP","slug":"you-cannot-ask-for-the-number-of-occurances-of-nul","errorCode":null,"errorMessage":"You cannot ask for the number of occurances of null.","messagePattern":"You cannot ask for the number of occurances of null\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/DirichletProcess.java","lineNumber":34,"sourceCode":"  public DirichletProcess(ProbabilityDistribution<E> baseMeasure, double alpha) {\n    this.baseMeasure = baseMeasure;\n    this.alpha = alpha;\n    this.sampled = new ClassicCounter<>();\n    sampled.incrementCount(null, alpha);\n  }\n\n  public E drawSample(Random random) {\n    E drawn = Counters.sample(sampled);\n    if (drawn == null) {\n      drawn = baseMeasure.drawSample(random);\n    }\n    sampled.incrementCount(drawn);\n    return drawn;\n  }\n\n  public double numOccurances(E object) {\n    if (object == null) {\n      throw new RuntimeException(\"You cannot ask for the number of occurances of null.\");\n    }\n    return sampled.getCount(object);\n  }\n  \n  public double probabilityOf(E object) {\n    if (object == null) {\n      throw new RuntimeException(\"You cannot ask for the probability of null.\");\n    }\n\n    if (sampled.keySet().contains(object)) {\n      return sampled.getCount(object) / sampled.totalCount();\n    } else {\n      return 0.0;\n    }\n  }\n\n  public double logProbabilityOf(E object) {\n    return Math.log(probabilityOf(object));","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/DirichletProcess.java#L16-L52","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check the object for null before calling and return 0 (a null object has never been sampled).","Fix the upstream code that produced a null object reference (failed parse, missing map entry).","Catch the RuntimeException at the call site only if null inputs are expected and can be treated as count 0."],"exampleFix":"// before\ndouble n = dp.numOccurances(token); // token may be null\n// after\ndouble n = (token == null) ? 0.0 : dp.numOccurances(token);","handlingStrategy":"type-guard","validationCode":"if (object != null) {\n  double n = dp.numOccurances(object);\n}","typeGuard":"boolean isNonNull(Object o) { return o != null; }\n// use: if (isNonNull(obj)) dp.numOccurances(obj);","tryCatchPattern":"double n;\ntry {\n  n = dp.numOccurances(object);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"occurances of null\")) n = 0.0; // null never sampled\n  else throw e;\n}","preventionTips":["Null-check objects before any DirichletProcess query.","Treat null as 'never sampled' (count 0) at the call site.","Fix upstream parsers/lookups so they do not emit null tokens."],"tags":["null-argument","statistics","validation"],"backgroundTag":"null-argument","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}