{"record":{"id":"81f7128c5b146f46","repo":"stanfordnlp/CoreNLP","slug":"arrays-do-not-have-the-same-length","errorCode":null,"errorMessage":"Arrays do not have the same length.","messagePattern":"Arrays do not have the same length\\.","errorType":"exception","errorClass":"ArrayIndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/ArrayMath.java","lineNumber":372,"sourceCode":"\n  /**\n   * Add the two 2d arrays and write the answer in place of {@code m1}.\n   *\n   * @throws IllegalArgumentException If {@code m1} and {@code m2} are not of the same dimensions\n   */\n  public static void addInPlace(double[][] m1, double[][] m2) {\n    if (m1.length != m2.length) {\n      throw new IllegalArgumentException();\n    }\n    for (int i = 0; i < m1.length; i++) {\n      pairwiseAddInPlace(m1[i], m2[i]);\n    }\n  }\n\n\n  public static void pairwiseSubtractInPlace(double[] to, double[] from) {\n    if (to.length != from.length) {\n      throw new ArrayIndexOutOfBoundsException(\"Arrays do not have the same length.\");\n    }\n    for (int i = 0; i < to.length; i++) {\n      to[i] -= from[i];\n    }\n  }\n\n  public static void pairwiseScaleAddInPlace(double[] to, double[] from, double fromScale) {\n    if (to.length != from.length) {\n      throw new ArrayIndexOutOfBoundsException(\"Arrays do not have the same length.\");\n    }\n    for (int i = 0; i < to.length; i++) {\n      to[i] += fromScale * from[i];\n    }\n  }\n\n  // OPERATIONS WITH TWO ARRAYS - NONDESTRUCTIVE\n\n  public static int[] pairwiseAdd(int[] a, int[] b) {","sourceCodeStart":354,"sourceCodeEnd":390,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/ArrayMath.java#L354-L390","documentation":"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.","triggerScenarios":"Calling pairwiseSubtractInPlace(double[], double[]) with vectors of different dimensionality, e.g. subtracting a baseline of a different size.","commonSituations":"Computing deltas between model versions with changed feature counts; differencing vectors extracted from mismatched configurations.","solutions":["Check to.length == from.length before calling and resize/align the arrays.","Catch ArrayIndexOutOfBoundsException around subtraction and treat it as a dimensionality bug, not an index bug.","Regenerate both vectors from the same feature pipeline."],"exampleFix":"// before\nArrayMath.pairwiseSubtractInPlace(a, b);\n// after\nif (a.length != b.length) throw new IllegalStateException(\"dim mismatch\");\nArrayMath.pairwiseSubtractInPlace(a, b);","handlingStrategy":"validation","validationCode":"if (to.length != from.length) throw new IllegalStateException(\"cannot pairwise-subtract: \" + to.length + \" vs \" + from.length);","typeGuard":"boolean sameLength(double[] a, double[] b) { return a.length == b.length; }","tryCatchPattern":"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; }","preventionTips":["Compare baseline and target vectors built from identical configs","Note this API throws AIOOBE for length errors — catch it explicitly","Add pre-call length checks in vector-difference utilities"],"tags":["java","arrays","dimension-mismatch"],"backgroundTag":"shape-mismatch","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}