DrKLO/Telegram · error · IndexOutOfBoundsException

{index} is an invalid index for size {size}

Error message

{index} is an invalid index for size {size}

What it means

getItemDecorationAt(index) bounds-checks against the decoration list size. If index < 0 or >= getItemDecorationCount(), it throws IndexOutOfBoundsException. Used to retrieve a previously-added ItemDecoration by position.

Source

Thrown at TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java:1665

     *
     * @param decor Decoration to add
     */
    public void addItemDecoration(@NonNull ItemDecoration decor) {
        addItemDecoration(decor, -1);
    }

    /**
     * Returns an {@link ItemDecoration} previously added to this RecyclerView.
     *
     * @param index The index position of the desired ItemDecoration.
     * @return the ItemDecoration at index position
     * @throws IndexOutOfBoundsException on invalid index
     */
    @NonNull
    public ItemDecoration getItemDecorationAt(int index) {
        final int size = getItemDecorationCount();
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException(index + " is an invalid index for size " + size);
        }

        return mItemDecorations.get(index);
    }

    /**
     * Returns the number of {@link ItemDecoration} currently added to this RecyclerView.
     *
     * @return number of ItemDecorations currently added added to this RecyclerView.
     */
    public int getItemDecorationCount() {
        return mItemDecorations.size();
    }

    /**
     * Removes the {@link ItemDecoration} associated with the supplied index position.
     *
     * @param index The index position of the ItemDecoration to be removed.

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Check 0 <= index && index < recyclerView.getItemDecorationCount() before calling.
  2. Iterate with for (int i = 0; i < count; i++) (strict less-than).
  3. Avoid hard-coded indices; track decoration references at add time.

Example fix

// before
ItemDecoration d = recyclerView.getItemDecorationAt(1); // maybe none

// after
if (recyclerView.getItemDecorationCount() > 1) {
    ItemDecoration d = recyclerView.getItemDecorationAt(1);
}
Defensive patterns

Strategy: validation

Validate before calling

if (index >= 0 && index < recyclerView.getItemDecorationCount()) {
    ItemDecoration d = recyclerView.getItemDecorationAt(index);
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Calling getItemDecorationAt with a hard-coded index larger than the number of decorations added, or a negative index. Iterating with an off-by-one upper bound (<= count instead of < count).

Common situations: Assuming a default decoration exists when none was added. Index computed from another source (e.g. adapter position) and passed unchecked.

Related errors


AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14). Data as JSON: /api/errors/ca58dd6c1ce2530b. Report an issue: GitHub.