{"record":{"id":"b1fc7646943d5158","repo":"stanfordnlp/CoreNLP","slug":"index-out-of-bounds","errorCode":null,"errorMessage":"Index out of bounds: ","messagePattern":"Index out of bounds: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/crf/CRFCliqueTree.java","lineNumber":111,"sourceCode":"  public double scoreOf(int[] sequence, int pos) {\n    return scoresOf(sequence, pos)[sequence[pos]];\n  }\n\n  /**\n   * Computes the unnormalized log conditional distribution over values of the\n   * element at position pos in the sequence, conditioned on the values of the\n   * elements in all other positions of the provided sequence.\n   *\n   * @param sequence\n   *          the sequence containing the rest of the values to condition on\n   * @param position\n   *          the position of the element to give a distribution for\n   * @return an array of type double, representing a probability distribution;\n   *         sums to 1.0\n   */\n  @Override\n  public double[] scoresOf(int[] sequence, int position) {\n    if (position >= factorTables.length) throw new RuntimeException(\"Index out of bounds: \" + position);\n    // DecimalFormat nf = new DecimalFormat(\"#0.000\");\n    // if (position>0 && position<sequence.length-1) System.out.println(position\n    // + \": asking about \" +sequence[position-1] + \"(\" + sequence[position] +\n    // \")\" + sequence[position+1]);\n    double[] probThisGivenPrev = new double[numClasses];\n    double[] probNextGivenThis = new double[numClasses];\n    // double[] marginal = new double[numClasses]; // for debugging only\n\n    // compute prob of this tag given the window-1 previous tags, normalized\n    // extract the window-1 previous tags, pad left with background if necessary\n    int prevLength = windowSize - 1;\n    int[] prev = new int[prevLength + 1]; // leave an extra element for the\n    // label at this position\n    int i = 0;\n    for (; i < prevLength - position; i++) { // will only happen if\n      // position-prevLength < 0\n      prev[i] = classIndex.indexOf(backgroundSymbol);\n    }","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/crf/CRFCliqueTree.java#L93-L129","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Clamp or bound-check position before calling: ensure 0 <= position < sequence.length (and sequence is non-empty).","Fix the caller loop's upper bound (e.g. i < sequence.length, not <=).","Guard against empty inputs before invoking classify/score APIs.","Rebuild the clique tree from the current sequence if the input changed after construction."],"exampleFix":"// before\ndouble[] scores = cliqueTree.scoresOf(seq, i); // i can equal seq.length\n// after\nif (seq.length == 0 || i < 0 || i >= seq.length) continue;\ndouble[] scores = cliqueTree.scoresOf(seq, i);","handlingStrategy":"validation","validationCode":"if (sequence == null || sequence.length == 0)\n  throw new IllegalArgumentException(\"empty sequence\");\nif (position < 0 || position >= sequence.length)\n  throw new IllegalArgumentException(\"position \" + position + \" out of [0,\" + sequence.length + \")\");","typeGuard":"static boolean validPosition(int[] sequence, int position) {\n  return sequence != null && position >= 0 && position < sequence.length;\n}","tryCatchPattern":"try {\n  scores = cliqueTree.scoresOf(sequence, position);\n} catch (RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"Index out of bounds: \")) {\n    position = Math.min(position, sequence.length - 1);\n    scores = position >= 0 ? cliqueTree.scoresOf(sequence, position) : new double[0];\n  } else throw e;\n}","preventionTips":["Reject empty input documents before classification.","Use strict bounds (i < length) in loops over positions.","Rebuild clique trees when the underlying sequence changes."],"tags":["index-out-of-bounds","sequence","inference","crf"],"backgroundTag":"index-out-of-bounds","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}