stanfordnlp/CoreNLP · error · ArrayIndexOutOfBoundsException

Index outside the bounds

Error message

Index ${i} outside the bounds [0,${size()})

What it means

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()).

Solutions

  1. Bounds-check i against index.size() before calling get
  2. Ensure the integer ID came from the same Index instance (indexOf(...)) that you call get on
  3. Do not cache indices across modifications; re-derive them after changes

Example fix

// before
E e = index.get(i); // may throw
// after
if (i >= 0 && i < index.size()) { E e = index.get(i); } else { /* handle invalid index */ }
Defensive patterns

Strategy: type-guard

Validate before calling

boolean inBounds = i >= 0 && i < index.size();

Type guard

boolean validIndex(Index<?> idx, int i) { return i >= 0 && i < idx.size(); }

Try / catch

try { E e = index.get(i); } catch (ArrayIndexOutOfBoundsException e) { /* re-derive the index via index.indexOf(obj) */ }

Prevention

When it happens

Trigger: 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.

Common situations: Mixing two HashIndex instances (IDs from one used on another); caching indices across a clear()/modification; off-by-one loops over size().

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/util/HashIndex.java:106

   * Returns the number of indexed objects.
   *
   * @return the number of indexed objects.
   */
  @Override
  public int size() {
    return objects.size();
  }

  /**
   * Gets the object whose index is the integer argument.
   *
   * @param i the integer index to be queried for the corresponding argument
   * @return the object whose index is the integer argument.
   */
  @Override
  public E get(int i) {
    if (i < 0 || i >= objects.size())
      throw new ArrayIndexOutOfBoundsException("Index " + i +
                                               " outside the bounds [0," +
                                               size() + ")");
    return objects.get(i);
  }

  /**
   * Returns a complete {@link List} of indexed objects, in the order of their indices.  <b>DANGER!</b>
   * The current implementation returns the actual index list, not a defensive copy.  Messing with this List
   * can seriously screw up the state of the Index.  (perhaps this method needs to be eliminated? I don't think it's
   * ever used in ways that we couldn't use the Index itself for directly.  --Roger, 12/29/04)
   *
   * @return a complete {@link List} of indexed objects
   */
  @Override
  public List<E> objectsList() {
    return objects;
  }

View on GitHub (pinned to 1b7edd19c4)