stanfordnlp/CoreNLP · error · IllegalArgumentException

Unknown index for Natural Logic relation: " + index

Error message

Unknown index for Natural Logic relation: " + index

What it means

NaturalLogicRelation.byFixedIndex() maps integers 0-6 to the six natural logic relations plus equality; any other index has no corresponding relation, so an IllegalArgumentException is thrown. The index space is fixed by the enum's internal ordering.

Solutions

  1. Validate the index is in [0,6] before calling byFixedIndex.
  2. Prefer using the enum constants or NaturalLogicRelation.byName-ish lookups instead of raw indices.
  3. If indices come from stored data, re-serialize them with the current library version and confirm the enum ordering.

Example fix

// before
NaturalLogicRelation rel = NaturalLogicRelation.byFixedIndex(idx);
// after
if (idx < 0 || idx > 6) throw new IllegalArgumentException("relation index out of range: " + idx);
NaturalLogicRelation rel = NaturalLogicRelation.byFixedIndex(idx);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0 || index > 6) throw new IllegalArgumentException("Natural logic relation index must be 0-6: " + index);

Try / catch

try {
  rel = NaturalLogicRelation.byFixedIndex(idx);
} catch (IllegalArgumentException e) {
  rel = NaturalLogicRelation.INDEPENDENCE; // or rethrow with context
}

Prevention

When it happens

Trigger: Calling byFixedIndex with an integer outside 0-6, e.g. a deserialized relation index from a corrupt/mismatched model file or an off-by-one when converting projective join table results.

Common situations: Persisting relation indices with one version of the library and reading them with another whose enum ordering changed; manual index arithmetic when composing relations.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/naturalli/NaturalLogicRelation.java:82

  NaturalLogicRelation(int fixedIndex, boolean maintainsTruth, boolean negatesTruth, boolean maintainsFalsehood, boolean negatesFalsehood) {
    this.fixedIndex = fixedIndex;
    this.maintainsTruth = maintainsTruth;
    this.negatesTruth = negatesTruth;
    this.maintainsFalsehood = maintainsFalsehood;
    this.negatesFalsehood = negatesFalsehood;
  }

  protected static NaturalLogicRelation byFixedIndex(int index) {
    switch (index) {
      case 0: return EQUIVALENT;
      case 1: return FORWARD_ENTAILMENT;
      case 2: return REVERSE_ENTAILMENT;
      case 3: return NEGATION;
      case 4: return ALTERNATION;
      case 5: return COVER;
      case 6: return INDEPENDENCE;
      default: throw new IllegalArgumentException("Unknown index for Natural Logic relation: " + index);
    }
  }

  /**
   * The MacCartney "join table" -- this determines the transitivity of entailment if we chain two relations together.
   * These should already be projected up through the sentence, so that the relations being joined are relations between
   * <i>sentences</i> rather than relations between <i>lexical items</i> (see {@link Polarity#projectLexicalRelation(NaturalLogicRelation)},
   * set by {@link edu.stanford.nlp.naturalli.NaturalLogicAnnotator} using the {@link edu.stanford.nlp.naturalli.NaturalLogicAnnotations.PolarityAnnotation}).
   * @param other The relation to join this relation with.
   * @return The new joined relation.
   */
  public NaturalLogicRelation join(NaturalLogicRelation other) {
    switch (this) {
      case EQUIVALENT:
        return other;
      case FORWARD_ENTAILMENT:
        switch (other) {
          case EQUIVALENT:

View on GitHub (pinned to 1b7edd19c4)