stanfordnlp/CoreNLP · error · RuntimeException

Index out of bounds:

Error message

Index out of bounds: 

What it means

CRFCliqueTree.scoresOf computes the score distribution over classes at a given sequence position. Because position indexes the factor tables (one per sequence position), any position >= factorTables.length is invalid and the method throws this RuntimeException instead of an ArrayIndexOutOfBoundsException.

Solutions

  1. Clamp or bound-check position before calling: ensure 0 <= position < sequence.length (and sequence is non-empty).
  2. Fix the caller loop's upper bound (e.g. i < sequence.length, not <=).
  3. Guard against empty inputs before invoking classify/score APIs.
  4. Rebuild the clique tree from the current sequence if the input changed after construction.

Example fix

// before
double[] scores = cliqueTree.scoresOf(seq, i); // i can equal seq.length
// after
if (seq.length == 0 || i < 0 || i >= seq.length) continue;
double[] scores = cliqueTree.scoresOf(seq, i);
Defensive patterns

Strategy: validation

Validate before calling

if (sequence == null || sequence.length == 0)
  throw new IllegalArgumentException("empty sequence");
if (position < 0 || position >= sequence.length)
  throw new IllegalArgumentException("position " + position + " out of [0," + sequence.length + ")");

Type guard

static boolean validPosition(int[] sequence, int position) {
  return sequence != null && position >= 0 && position < sequence.length;
}

Try / catch

try {
  scores = cliqueTree.scoresOf(sequence, position);
} catch (RuntimeException e) {
  if (e.getMessage() != null && e.getMessage().startsWith("Index out of bounds: ")) {
    position = Math.min(position, sequence.length - 1);
    scores = position >= 0 ? cliqueTree.scoresOf(sequence, position) : new double[0];
  } else throw e;
}

Prevention

When it happens

Trigger: Calling scoresOf(sequence, position) (directly or via scoreOf/result) on a CRFCliqueTree with position >= number of positions in the sequence/factorTables — e.g. a 0-length or empty sequence with position 0, or iterating past sequence end.

Common situations: Off-by-one loops over sequence positions, calling the classifier on empty input documents, custom decoding code reusing a stale tree with a longer position index, or sequence mutation after the clique tree was built.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ie/crf/CRFCliqueTree.java:111

  public double scoreOf(int[] sequence, int pos) {
    return scoresOf(sequence, pos)[sequence[pos]];
  }

  /**
   * Computes the unnormalized log conditional distribution over values of the
   * element at position pos in the sequence, conditioned on the values of the
   * elements in all other positions of the provided sequence.
   *
   * @param sequence
   *          the sequence containing the rest of the values to condition on
   * @param position
   *          the position of the element to give a distribution for
   * @return an array of type double, representing a probability distribution;
   *         sums to 1.0
   */
  @Override
  public double[] scoresOf(int[] sequence, int position) {
    if (position >= factorTables.length) throw new RuntimeException("Index out of bounds: " + position);
    // DecimalFormat nf = new DecimalFormat("#0.000");
    // if (position>0 && position<sequence.length-1) System.out.println(position
    // + ": asking about " +sequence[position-1] + "(" + sequence[position] +
    // ")" + sequence[position+1]);
    double[] probThisGivenPrev = new double[numClasses];
    double[] probNextGivenThis = new double[numClasses];
    // double[] marginal = new double[numClasses]; // for debugging only

    // compute prob of this tag given the window-1 previous tags, normalized
    // extract the window-1 previous tags, pad left with background if necessary
    int prevLength = windowSize - 1;
    int[] prev = new int[prevLength + 1]; // leave an extra element for the
    // label at this position
    int i = 0;
    for (; i < prevLength - position; i++) { // will only happen if
      // position-prevLength < 0
      prev[i] = classIndex.indexOf(backgroundSymbol);
    }

View on GitHub (pinned to 1b7edd19c4)