stanfordnlp/CoreNLP · error · ArrayIndexOutOfBoundsException

Arrays do not have the same length.

Error message

Arrays do not have the same length.

What it means

ArrayMath.pairwiseSubtractInPlace subtracts from[i] from to[i] in place and throws ArrayIndexOutOfBoundsException (despite being a length check, not an actual index violation) when the arrays differ in length.

Solutions

  1. Check to.length == from.length before calling and resize/align the arrays.
  2. Catch ArrayIndexOutOfBoundsException around subtraction and treat it as a dimensionality bug, not an index bug.
  3. Regenerate both vectors from the same feature pipeline.

Example fix

// before
ArrayMath.pairwiseSubtractInPlace(a, b);
// after
if (a.length != b.length) throw new IllegalStateException("dim mismatch");
ArrayMath.pairwiseSubtractInPlace(a, b);
Defensive patterns

Strategy: validation

Validate before calling

if (to.length != from.length) throw new IllegalStateException("cannot pairwise-subtract: " + to.length + " vs " + from.length);

Type guard

boolean sameLength(double[] a, double[] b) { return a.length == b.length; }

Try / catch

try { ArrayMath.pairwiseSubtractInPlace(to, from); } catch (ArrayIndexOutOfBoundsException e) { if ("Arrays do not have the same length.".equals(e.getMessage())) { /* treat as dimension mismatch */ } else throw e; }

Prevention

When it happens

Trigger: Calling pairwiseSubtractInPlace(double[], double[]) with vectors of different dimensionality, e.g. subtracting a baseline of a different size.

Common situations: Computing deltas between model versions with changed feature counts; differencing vectors extracted from mismatched configurations.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/math/ArrayMath.java:372

  /**
   * Add the two 2d arrays and write the answer in place of {@code m1}.
   *
   * @throws IllegalArgumentException If {@code m1} and {@code m2} are not of the same dimensions
   */
  public static void addInPlace(double[][] m1, double[][] m2) {
    if (m1.length != m2.length) {
      throw new IllegalArgumentException();
    }
    for (int i = 0; i < m1.length; i++) {
      pairwiseAddInPlace(m1[i], m2[i]);
    }
  }


  public static void pairwiseSubtractInPlace(double[] to, double[] from) {
    if (to.length != from.length) {
      throw new ArrayIndexOutOfBoundsException("Arrays do not have the same length.");
    }
    for (int i = 0; i < to.length; i++) {
      to[i] -= from[i];
    }
  }

  public static void pairwiseScaleAddInPlace(double[] to, double[] from, double fromScale) {
    if (to.length != from.length) {
      throw new ArrayIndexOutOfBoundsException("Arrays do not have the same length.");
    }
    for (int i = 0; i < to.length; i++) {
      to[i] += fromScale * from[i];
    }
  }

  // OPERATIONS WITH TWO ARRAYS - NONDESTRUCTIVE

  public static int[] pairwiseAdd(int[] a, int[] b) {

View on GitHub (pinned to 1b7edd19c4)