{"record":{"id":"a740657d157bda17","repo":"stanfordnlp/CoreNLP","slug":"not-a-valid-object-for-this-multinomial","errorCode":null,"errorMessage":"Not a valid object for this multinomial!","messagePattern":"Not a valid object for this multinomial!","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/Multinomial.java","lineNumber":44,"sourceCode":"    }\n\n    this.parameters = new ClassicCounter<>();\n    for (E object : parameters.keySet()) {\n      double oldCount = parameters.getCount(object);\n      if (oldCount < 0.0) {\n        throw new RuntimeException(\"no negative parameters allowed!\");\n      }\n      this.parameters.setCount(object, oldCount/totalMass);\n    }\n  }\n\n  public Counter<E> getParameters() {\n    return new ClassicCounter<>(parameters);\n  }\n  \n  public double probabilityOf(E object) {\n    if (!parameters.keySet().contains(object)) {\n      throw new RuntimeException(\"Not a valid object for this multinomial!\");\n    }\n    return parameters.getCount(object);\n  }\n\n  public double logProbabilityOf(E object) {\n    if (!parameters.keySet().contains(object)) {\n      throw new RuntimeException(\"Not a valid object for this multinomial!\");\n    }\n    return Math.log(parameters.getCount(object));\n  }\n\n  public E drawSample(Random random) {\n    double r = random.nextDouble();\n    double sum = 0.0;\n    for (E object : parameters.keySet()) {\n      sum += parameters.getCount(object);\n      if (sum  >= r) {\n        return object;","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/Multinomial.java#L26-L62","documentation":"probabilityOf(object) requires the object to be a key of the multinomial; the library throws rather than returning 0 so callers don't mistake a missing key for a genuine zero probability. It is also called internally by sampleBeta during sampling.","triggerScenarios":"Calling probabilityOf(o) where the multinomial's parameter counter does not contain o; drawing a sample internally whose value isn't in the key set (stale parameters); using an object of the wrong generic type that hashes differently.","commonSituations":"Querying probabilities for vocabulary items that were never added; sharing a Multinomial after rebuilding the underlying counter; comparing Strings vs other key types across tokenizers.","solutions":["Check parameters().containsKey(object) (or getParameters().containsKey) before calling probabilityOf and define a fallback (0.0 or smoothed value)","Use logProbabilityOf with an explicit contains-check or catch the exception if absent objects are expected","Ensure the Multinomial is built over the same key set you later query"],"exampleFix":"// before\ndouble p = m.probabilityOf(word);\n// after\ndouble p = m.getParameters().containsKey(word) ? m.probabilityOf(word) : 0.0;","handlingStrategy":"try-catch","validationCode":"if (!m.getParameters().containsKey(object)) {\n  return 0.0; // or smoothed default\n}","typeGuard":"boolean isInSupport(Multinomial<E> m, E o) { return m.getParameters().containsKey(o); }","tryCatchPattern":"try {\n  return m.probabilityOf(o);\n} catch (RuntimeException e) {\n  if (\"Not a valid object for this multinomial!\".equals(e.getMessage())) {\n    return 0.0;\n  }\n  throw e;\n}","preventionTips":["Build the multinomial over the same vocabulary you query","Use containsKey checks instead of relying on exceptions","Keep a single canonical vocabulary object across construction and querying"],"tags":["java","statistics","lookup"],"backgroundTag":"resource-not-found","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"}