{"record":{"id":"59125cdc8c3d93b1","repo":"material-components/material-components-android","slug":"index-out-of-range","errorCode":null,"errorMessage":"index out of range","messagePattern":"index out of range","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/slider/BaseSlider.java","lineNumber":1168,"sourceCode":"    int originalHeight = drawable.getIntrinsicHeight();\n    if (originalWidth == -1 && originalHeight == -1) {\n      drawable.setBounds(0, 0, width, thumbHeight);\n    } else {\n      float scaleRatio = (float) max(width, thumbHeight) / max(originalWidth, originalHeight);\n      drawable.setBounds(\n          0, 0, (int) (originalWidth * scaleRatio), (int) (originalHeight * scaleRatio));\n    }\n  }\n\n  /** Returns the index of the currently focused thumb */\n  public int getFocusedThumbIndex() {\n    return focusedThumbIdx;\n  }\n\n  /** Sets the index of the currently focused thumb */\n  public void setFocusedThumbIndex(int index) {\n    if (index < 0 || index >= values.size()) {\n      throw new IllegalArgumentException(\"index out of range\");\n    }\n    focusedThumbIdx = index;\n    accessibilityHelper.requestKeyboardFocusForVirtualView(focusedThumbIdx);\n    postInvalidate();\n  }\n\n  protected void setActiveThumbIndex(int index) {\n    activeThumbIdx = index;\n  }\n\n  /** Returns the index of the currently active thumb, or -1 if no thumb is active */\n  public int getActiveThumbIndex() {\n    return activeThumbIdx;\n  }\n\n  /**\n   * Registers a callback to be invoked when the slider changes. On the RangeSlider implementation,\n   * the listener is invoked once for each value.","sourceCodeStart":1150,"sourceCodeEnd":1186,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/slider/BaseSlider.java#L1150-L1186","documentation":"IllegalArgumentException thrown synchronously by setFocusedThumbIndex(int) when the index is negative or >= values.size(). The focused thumb is the one highlighted and driven by keyboard/a11y interaction, so it must reference an existing thumb.","triggerScenarios":"Calling setFocusedThumbIndex(1) on a single-value slider; caching a focus index across a call to setValues() that reduces the thumb count; passing -1 as 'no focus' (use clearFocusedThumbIndex-equivalent behavior or skip the call instead).","commonSituations":"Restoring UI state after process death when the number of thumbs changed; accessibility flows that remember the last focused thumb; index math based on a stale values list.","solutions":["Bound-check before calling: 0 <= index < slider.getValues().size().","Re-apply focus after setValues(), not before, since values.size() may have changed.","Skip the call (or clamp to size-1) instead of passing -1 when 'no thumb' is desired."],"exampleFix":"// before\nslider.setFocusedThumbIndex(savedIndex); // crashes if thumb count shrank\n\n// after\nif (savedIndex >= 0 && savedIndex < slider.getValues().size()) {\n  slider.setFocusedThumbIndex(savedIndex);\n}","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < slider.getValues().size()) {\n  slider.setFocusedThumbIndex(index);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Re-apply the focused index after setValues(), not before.","Persist the focused index together with the value count and validate on restore."],"tags":["android","slider","material-components","validation","immediate","accessibility"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}