{"record":{"id":"ca58dd6c1ce2530b","repo":"DrKLO/Telegram","slug":"index-is-an-invalid-index-for-size-size","errorCode":null,"errorMessage":"{index} is an invalid index for size {size}","messagePattern":"(.+?) is an invalid index for size (.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java","lineNumber":1665,"sourceCode":"     *\n     * @param decor Decoration to add\n     */\n    public void addItemDecoration(@NonNull ItemDecoration decor) {\n        addItemDecoration(decor, -1);\n    }\n\n    /**\n     * Returns an {@link ItemDecoration} previously added to this RecyclerView.\n     *\n     * @param index The index position of the desired ItemDecoration.\n     * @return the ItemDecoration at index position\n     * @throws IndexOutOfBoundsException on invalid index\n     */\n    @NonNull\n    public ItemDecoration getItemDecorationAt(int index) {\n        final int size = getItemDecorationCount();\n        if (index < 0 || index >= size) {\n            throw new IndexOutOfBoundsException(index + \" is an invalid index for size \" + size);\n        }\n\n        return mItemDecorations.get(index);\n    }\n\n    /**\n     * Returns the number of {@link ItemDecoration} currently added to this RecyclerView.\n     *\n     * @return number of ItemDecorations currently added added to this RecyclerView.\n     */\n    public int getItemDecorationCount() {\n        return mItemDecorations.size();\n    }\n\n    /**\n     * Removes the {@link ItemDecoration} associated with the supplied index position.\n     *\n     * @param index The index position of the ItemDecoration to be removed.","sourceCodeStart":1647,"sourceCodeEnd":1683,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java#L1647-L1683","documentation":"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.","triggerScenarios":"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).","commonSituations":"Assuming a default decoration exists when none was added. Index computed from another source (e.g. adapter position) and passed unchecked.","solutions":["Check 0 <= index && index < recyclerView.getItemDecorationCount() before calling.","Iterate with for (int i = 0; i < count; i++) (strict less-than).","Avoid hard-coded indices; track decoration references at add time."],"exampleFix":"// before\nItemDecoration d = recyclerView.getItemDecorationAt(1); // maybe none\n\n// after\nif (recyclerView.getItemDecorationCount() > 1) {\n    ItemDecoration d = recyclerView.getItemDecorationAt(1);\n}","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < recyclerView.getItemDecorationCount()) {\n    ItemDecoration d = recyclerView.getItemDecorationAt(index);\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Bounds-check against getItemDecorationCount before indexing.","Hold references to decorations at add-time instead of looking them up by index.","Iterate with strict < count."],"tags":["recyclerview","itemdecoration","bounds-check","validation"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}