stanfordnlp/CoreNLP · error · IllegalArgumentException

Not a valid index: " + index

Error message

Not a valid index: " + index

What it means

ClauseSplitter.clauseClassifierLabelToValue / fromIndex maps small integer codes (0..2) to ClauseClassifierLabel enum values. Any index outside 0-2 has no label, so it throws IllegalArgumentException — a strict enum decode guard.

Solutions

  1. Validate the index is in [0,2] before conversion and map out-of-range values to NOT_A_CLAUSE explicitly
  2. Check the source of the integer (model output, annotation) for changed label semantics across versions
  3. Regenerate any cached/persisted labels with the current library version

Example fix

// before
ClauseClassifierLabel label = ClauseSplitter.clauseClassifierLabelToValue(rawIndex);
// after
ClauseClassifierLabel label = (rawIndex >= 0 && rawIndex <= 2)
    ? ClauseSplitter.clauseClassifierLabelToValue(rawIndex)
    : ClauseClassifierLabel.NOT_A_CLAUSE;
Defensive patterns

Strategy: type-guard

Validate before calling

if (index < 0 || index > 2) { throw new IllegalArgumentException("Clause label index out of range: " + index); }

Type guard

boolean isValidClauseLabelIndex(int i) { return i >= 0 && i <= 2; }

Try / catch

try { label = ClauseSplitter.clauseClassifierLabelToValue(idx); } catch (IllegalArgumentException e) { label = ClauseClassifierLabel.NOT_A_CLAUSE; }

Prevention

When it happens

Trigger: Calling the fromIndex-style conversion with an integer other than 0, 1, or 2 — typically from a model file, annotation key, or serialized prediction containing an out-of-range code (e.g. -1 sentinel for missing labels).

Common situations: Model/annotation files saved with a different label vocabulary; code storing -1 as 'unknown' then feeding it to the converter; version drift between the classifier label set and the reader.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/naturalli/ClauseSplitter.java:63

    ClauseClassifierLabel(int val) {
      this.index = (byte) val;
    }
    /** Seriously, why would Java not have this by default? */
    @Override
    public String toString() {
      return this.name();
    }
    @SuppressWarnings("unused")
    public static ClauseClassifierLabel fromIndex(int index) {
      switch (index) {
        case 0:
          return NOT_A_CLAUSE;
        case 1:
          return CLAUSE_INTERM;
        case 2:
          return CLAUSE_SPLIT;
        default:
          throw new IllegalArgumentException("Not a valid index: " + index);
      }
    }
  }


  /**
   * Train a clause searcher factory. That is, train a classifier for which arcs should be
   * new clauses.
   *
   * @param trainingData The training data. This is a stream of triples of:
   *                     <ol>
   *                       <li>The sentence containing a known extraction.</li>
   *                       <li>The span of the subject in the sentence, as a token span.</li>
   *                       <li>The span of the object in the sentence, as a token span.</li>
   *                     </ol>
   * @param modelPath The path to save the model to. This is useful for {@link ClauseSplitter#load(String)}.
   * @param trainingDataDump The path to save the training data, as a set of labeled featurized datums.
   * @param featurizer The featurizer to use for this classifier.

View on GitHub (pinned to 1b7edd19c4)