{"record":{"id":"e9a0a2a619271c1a","repo":"stanfordnlp/CoreNLP","slug":"index-i-outside-the-bounds-0-size","errorCode":null,"errorMessage":"Index ${i} outside the bounds [0,${size()})","messagePattern":"Index (.+?) outside the bounds \\[0,(.+?)\\)","errorType":"exception","errorClass":"ArrayIndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/HashIndex.java","lineNumber":106,"sourceCode":"   * Returns the number of indexed objects.\n   *\n   * @return the number of indexed objects.\n   */\n  @Override\n  public int size() {\n    return objects.size();\n  }\n\n  /**\n   * Gets the object whose index is the integer argument.\n   *\n   * @param i the integer index to be queried for the corresponding argument\n   * @return the object whose index is the integer argument.\n   */\n  @Override\n  public E get(int i) {\n    if (i < 0 || i >= objects.size())\n      throw new ArrayIndexOutOfBoundsException(\"Index \" + i +\n                                               \" outside the bounds [0,\" +\n                                               size() + \")\");\n    return objects.get(i);\n  }\n\n  /**\n   * Returns a complete {@link List} of indexed objects, in the order of their indices.  <b>DANGER!</b>\n   * The current implementation returns the actual index list, not a defensive copy.  Messing with this List\n   * can seriously screw up the state of the Index.  (perhaps this method needs to be eliminated? I don't think it's\n   * ever used in ways that we couldn't use the Index itself for directly.  --Roger, 12/29/04)\n   *\n   * @return a complete {@link List} of indexed objects\n   */\n  @Override\n  public List<E> objectsList() {\n    return objects;\n  }\n","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/HashIndex.java#L88-L124","documentation":"HashIndex.get(int) retrieves the element at position i from the backing list. If i is negative or >= size(), it throws ArrayIndexOutOfBoundsException with this message showing the valid range [0,size()).","triggerScenarios":"Calling index.get(i) with i < 0 or i >= index.size(), typically using an index value from a different Index or from before items were added/removed.","commonSituations":"Mixing two HashIndex instances (IDs from one used on another); caching indices across a clear()/modification; off-by-one loops over size().","solutions":["Bounds-check i against index.size() before calling get","Ensure the integer ID came from the same Index instance (indexOf(...)) that you call get on","Do not cache indices across modifications; re-derive them after changes"],"exampleFix":"// before\nE e = index.get(i); // may throw\n// after\nif (i >= 0 && i < index.size()) { E e = index.get(i); } else { /* handle invalid index */ }","handlingStrategy":"type-guard","validationCode":"boolean inBounds = i >= 0 && i < index.size();","typeGuard":"boolean validIndex(Index<?> idx, int i) { return i >= 0 && i < idx.size(); }","tryCatchPattern":"try { E e = index.get(i); } catch (ArrayIndexOutOfBoundsException e) { /* re-derive the index via index.indexOf(obj) */ }","preventionTips":["Never reuse integer IDs across different Index instances","Re-derive indices after clear() or any mutation","Check size() before positional access in loops"],"tags":["index","bounds","arrayindexoutofbounds"],"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-17T15:17:12.973Z"}