{"record":{"id":"e22abc3dc4b9459d","repo":"Yalantis/uCrop","slug":"index-selectedbydefault-d-0-based-cannot-be","errorCode":null,"errorMessage":"Index [selectedByDefault = %d] (0-based) cannot be higher or equal than aspect ratio options count [count = %d].","messagePattern":"Index \\[selectedByDefault = (.+?)\\] \\(0-based\\) cannot be higher or equal than aspect ratio options count \\[count = (.+?)\\]\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/java/com/yalantis/ucrop/UCrop.java","lineNumber":540,"sourceCode":"            mOptionBundle.putBoolean(EXTRA_HIDE_BOTTOM_CONTROLS, hide);\n        }\n\n        /**\n         * @param enabled - set to true to let user resize crop bounds (disabled by default)\n         */\n        public void setFreeStyleCropEnabled(boolean enabled) {\n            mOptionBundle.putBoolean(EXTRA_FREE_STYLE_CROP, enabled);\n        }\n\n        /**\n         * Pass an ordered list of desired aspect ratios that should be available for a user.\n         *\n         * @param selectedByDefault - index of aspect ratio option that is selected by default (starts with 0).\n         * @param aspectRatio       - list of aspect ratio options that are available to user\n         */\n        public void setAspectRatioOptions(int selectedByDefault, AspectRatio... aspectRatio) {\n            if (selectedByDefault >= aspectRatio.length) {\n                throw new IllegalArgumentException(String.format(Locale.US,\n                        \"Index [selectedByDefault = %d] (0-based) cannot be higher or equal than aspect ratio options count [count = %d].\",\n                        selectedByDefault, aspectRatio.length));\n            }\n            mOptionBundle.putInt(EXTRA_ASPECT_RATIO_SELECTED_BY_DEFAULT, selectedByDefault);\n            mOptionBundle.putParcelableArrayList(EXTRA_ASPECT_RATIO_OPTIONS, new ArrayList<Parcelable>(Arrays.asList(aspectRatio)));\n        }\n\n        /**\n         * @param color - desired background color that should be applied to the root view\n         */\n        public void setRootViewBackgroundColor(@ColorInt int color) {\n            mOptionBundle.putInt(EXTRA_UCROP_ROOT_VIEW_BACKGROUND_COLOR, color);\n        }\n\n        /**\n         * Set an aspect ratio for crop bounds.\n         * User won't see the menu with other ratios options.\n         *","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/java/com/yalantis/ucrop/UCrop.java#L522-L558","documentation":"uCrop throws this IllegalArgumentException from UCrop.Options.setAspectRatioOptions() when the index of the aspect-ratio option to preselect is >= the number of provided options. Since indexes are 0-based, a valid selectedByDefault must be in [0, aspectRatio.length - 1]. The library fails fast because the bundle would otherwise point at a nonexistent option and crash later inside the crop UI.","triggerScenarios":"Calling UCrop.Options#setAspectRatioOptions(int selectedByDefault, AspectRatio...) with selectedByDefault equal to or larger than the number of AspectRatio arguments, including empty varargs (count = 0, any index throws).","commonSituations":"Building a list of aspect ratios dynamically and using its size (instead of size-1) as the preselected index; copying a sample snippet with more options than you pass; passing zero AspectRatio entries while still supplying an index.","solutions":["Ensure selectedByDefault is 0-based and strictly less than the number of AspectRatio values passed.","If preselecting dynamically, clamp the index: Math.min(index, aspectRatios.length - 1).","If passing an empty option list, either add at least one AspectRatio or skip calling setAspectRatioOptions entirely.","Recount the varargs: e.g. with three AspectRatio options, valid indexes are only 0, 1, or 2."],"exampleFix":"// before\noptions.setAspectRatioOptions(3,\n    new AspectRatio(\"1:1\", 1f, 1f),\n    new AspectRatio(\"4:3\", 4f, 3f),\n    new AspectRatio(\"16:9\", 16f, 9f));\n// after\noptions.setAspectRatioOptions(2,\n    new AspectRatio(\"1:1\", 1f, 1f),\n    new AspectRatio(\"4:3\", 4f, 3f),\n    new AspectRatio(\"16:9\", 16f, 9f));","handlingStrategy":"validation","validationCode":"if (aspectRatios.length == 0 || selectedByDefault < 0 || selectedByDefault >= aspectRatios.length) {\n    throw new IllegalArgumentException(\"selectedByDefault must be in [0, \" + (aspectRatios.length - 1) + \"]\");\n}\noptions.setAspectRatioOptions(selectedByDefault, aspectRatios.toArray(new AspectRatio[0]));","typeGuard":"boolean isValidSelection(int selectedByDefault, AspectRatio[] options) {\n    return options != null && options.length > 0 && selectedByDefault >= 0 && selectedByDefault < options.length;\n}","tryCatchPattern":"try {\n    options.setAspectRatioOptions(selectedByDefault, ratios);\n} catch (IllegalArgumentException e) {\n    options.setAspectRatioOptions(0, ratios);\n}","preventionTips":["Treat selectedByDefault as 0-based; the last valid index is count-1.","Never call setAspectRatioOptions with zero AspectRatio arguments.","When computing the index dynamically, clamp it against the array length.","Unit-test option configuration with the exact option list you ship."],"tags":["android","ucrop","illegal-argument","aspect-ratio","index-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}