stanfordnlp/CoreNLP · error · RuntimeException

Not a valid object for this multinomial!

Error message

Not a valid object for this multinomial!

What it means

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.

Solutions

  1. Check parameters().containsKey(object) (or getParameters().containsKey) before calling probabilityOf and define a fallback (0.0 or smoothed value)
  2. Use logProbabilityOf with an explicit contains-check or catch the exception if absent objects are expected
  3. Ensure the Multinomial is built over the same key set you later query

Example fix

// before
double p = m.probabilityOf(word);
// after
double p = m.getParameters().containsKey(word) ? m.probabilityOf(word) : 0.0;
Defensive patterns

Strategy: try-catch

Validate before calling

if (!m.getParameters().containsKey(object)) {
  return 0.0; // or smoothed default
}

Type guard

boolean isInSupport(Multinomial<E> m, E o) { return m.getParameters().containsKey(o); }

Try / catch

try {
  return m.probabilityOf(o);
} catch (RuntimeException e) {
  if ("Not a valid object for this multinomial!".equals(e.getMessage())) {
    return 0.0;
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/stats/Multinomial.java:44

    }

    this.parameters = new ClassicCounter<>();
    for (E object : parameters.keySet()) {
      double oldCount = parameters.getCount(object);
      if (oldCount < 0.0) {
        throw new RuntimeException("no negative parameters allowed!");
      }
      this.parameters.setCount(object, oldCount/totalMass);
    }
  }

  public Counter<E> getParameters() {
    return new ClassicCounter<>(parameters);
  }
  
  public double probabilityOf(E object) {
    if (!parameters.keySet().contains(object)) {
      throw new RuntimeException("Not a valid object for this multinomial!");
    }
    return parameters.getCount(object);
  }

  public double logProbabilityOf(E object) {
    if (!parameters.keySet().contains(object)) {
      throw new RuntimeException("Not a valid object for this multinomial!");
    }
    return Math.log(parameters.getCount(object));
  }

  public E drawSample(Random random) {
    double r = random.nextDouble();
    double sum = 0.0;
    for (E object : parameters.keySet()) {
      sum += parameters.getCount(object);
      if (sum  >= r) {
        return object;

View on GitHub (pinned to 1b7edd19c4)