{"record":{"id":"3a7d260f4c882056","repo":"stanfordnlp/CoreNLP","slug":"index-array-length","errorCode":null,"errorMessage":"Index array length ","messagePattern":"Index array length ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/semgraph/semgrex/Alignment.java","lineNumber":166,"sourceCode":"   * {@code SemanticGraph} to the given text (passage) SemanticGraph, using\n   * the given array of indexes.  The i'th node of the array should contain the\n   * index of the node in the text (passage) SemanticGraph to which the i'th\n   * node in the hypothesis SemanticGraph is aligned, or -1 if it is aligned to\n   * NO_WORD.\n   */\n  public static Alignment makeFromIndexArray(SemanticGraph txtGraph,\n                                             SemanticGraph hypGraph,\n                                             int[] indexes,\n                                             double score,\n                                             String justification) {\n    if (txtGraph == null || txtGraph.isEmpty())\n      throw new IllegalArgumentException(\"Invalid txtGraph \" + txtGraph);\n    if (hypGraph == null || hypGraph.isEmpty())\n      throw new IllegalArgumentException(\"Invalid hypGraph \" + hypGraph);\n    if (indexes == null)\n      throw new IllegalArgumentException(\"Null index array\");\n    if (indexes.length != hypGraph.size())\n      throw new IllegalArgumentException(\"Index array length \" + indexes.length +\n                                         \" does not match hypGraph size \" + hypGraph.size());\n    Map<IndexedWord, IndexedWord> map =\n      Generics.newHashMap();\n    for (int i = 0; i < indexes.length; i++) {\n      IndexedWord hypNode = hypGraph.getNodeByIndex(i);\n      IndexedWord txtNode = IndexedWord.NO_WORD;\n      if (indexes[i] >= 0)\n        txtNode = txtGraph.getNodeByIndex(indexes[i]);\n      map.put(hypNode, txtNode);\n    }\n    return new Alignment(map, score, justification);\n  }\n\n  public static Alignment makeFromIndexArray(SemanticGraph txtGraph,\n                                             SemanticGraph hypGraph,\n                                             int[] indexes) {\n    return makeFromIndexArray(txtGraph, hypGraph, indexes, 0.0, null);\n  }","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/semgraph/semgrex/Alignment.java#L148-L184","documentation":"Alignment.makeFromIndexArray throws IllegalArgumentException when the length of the index array does not equal hypGraph.size(). Each position i in the array aligns the hypothesis node with index i, so the array must cover every hypothesis node.","triggerScenarios":"Calling Alignment.makeFromIndexArray(txtGraph, hypGraph, indexes, ...) where indexes.length != hypGraph.size(), e.g. after the hypothesis graph was rebuilt or truncated after the array was computed.","commonSituations":"Graph preprocessed (nodes dropped, collapsed, or re-indexed) after alignment indexes were computed; array built from a different sentence or graph version; off-by-one when filtering empty nodes.","solutions":["Recompute the index array against the exact hypGraph instance passed in","Pass the graphs and indexes through the pipeline together so they cannot diverge","Assert indexes.length == hypGraph.size() in caller code before the call"],"exampleFix":"// before\nint[] indexes = computeIndexes(oldHypGraph);\nSemGraph hypGraph = preprocess(oldHypGraph);\nAlignment align = Alignment.makeFromIndexArray(txtGraph, hypGraph, indexes, j);\n// after\nint[] indexes = computeIndexes(hypGraph);\nif (indexes.length != hypGraph.size()) { throw new IllegalStateException(\"stale alignment indexes\"); }\nAlignment align = Alignment.makeFromIndexArray(txtGraph, hypGraph, indexes, j);","handlingStrategy":"validation","validationCode":"if (indexes == null || hypGraph == null || indexes.length != hypGraph.size()) throw new IllegalArgumentException(\"indexes length must equal hypGraph.size()\");","typeGuard":"boolean indexesMatchGraph(int[] indexes, SemGraph hypGraph) { return indexes != null && hypGraph != null && indexes.length == hypGraph.size(); }","tryCatchPattern":"try { Alignment a = Alignment.makeFromIndexArray(txtGraph, hypGraph, indexes, j); } catch (IllegalArgumentException e) { throw new IllegalStateException(\"Stale alignment indexes: \" + e.getMessage(), e); }","preventionTips":["Compute the index array from the same graph instance you pass to makeFromIndexArray","Recompute indexes after any graph preprocessing (collapse, filter, re-index)","Add an assertion on indexes.length == hypGraph.size() in pipeline code"],"tags":["java","argument-validation","semgrex"],"backgroundTag":"invalid-argument-value","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"}