Yalantis/uCrop · error · IllegalArgumentException

Index [selectedByDefault = %d] (0-based) cannot be higher or

Error message

Index [selectedByDefault = %d] (0-based) cannot be higher or equal than aspect ratio options count [count = %d].

What it means

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.

Source

Thrown at ucrop/src/main/java/com/yalantis/ucrop/UCrop.java:540

            mOptionBundle.putBoolean(EXTRA_HIDE_BOTTOM_CONTROLS, hide);
        }

        /**
         * @param enabled - set to true to let user resize crop bounds (disabled by default)
         */
        public void setFreeStyleCropEnabled(boolean enabled) {
            mOptionBundle.putBoolean(EXTRA_FREE_STYLE_CROP, enabled);
        }

        /**
         * Pass an ordered list of desired aspect ratios that should be available for a user.
         *
         * @param selectedByDefault - index of aspect ratio option that is selected by default (starts with 0).
         * @param aspectRatio       - list of aspect ratio options that are available to user
         */
        public void setAspectRatioOptions(int selectedByDefault, AspectRatio... aspectRatio) {
            if (selectedByDefault >= aspectRatio.length) {
                throw new IllegalArgumentException(String.format(Locale.US,
                        "Index [selectedByDefault = %d] (0-based) cannot be higher or equal than aspect ratio options count [count = %d].",
                        selectedByDefault, aspectRatio.length));
            }
            mOptionBundle.putInt(EXTRA_ASPECT_RATIO_SELECTED_BY_DEFAULT, selectedByDefault);
            mOptionBundle.putParcelableArrayList(EXTRA_ASPECT_RATIO_OPTIONS, new ArrayList<Parcelable>(Arrays.asList(aspectRatio)));
        }

        /**
         * @param color - desired background color that should be applied to the root view
         */
        public void setRootViewBackgroundColor(@ColorInt int color) {
            mOptionBundle.putInt(EXTRA_UCROP_ROOT_VIEW_BACKGROUND_COLOR, color);
        }

        /**
         * Set an aspect ratio for crop bounds.
         * User won't see the menu with other ratios options.
         *

View on GitHub (pinned to f788b534b4)

Solutions

  1. Ensure selectedByDefault is 0-based and strictly less than the number of AspectRatio values passed.
  2. If preselecting dynamically, clamp the index: Math.min(index, aspectRatios.length - 1).
  3. If passing an empty option list, either add at least one AspectRatio or skip calling setAspectRatioOptions entirely.
  4. Recount the varargs: e.g. with three AspectRatio options, valid indexes are only 0, 1, or 2.

Example fix

// before
options.setAspectRatioOptions(3,
    new AspectRatio("1:1", 1f, 1f),
    new AspectRatio("4:3", 4f, 3f),
    new AspectRatio("16:9", 16f, 9f));
// after
options.setAspectRatioOptions(2,
    new AspectRatio("1:1", 1f, 1f),
    new AspectRatio("4:3", 4f, 3f),
    new AspectRatio("16:9", 16f, 9f));
Defensive patterns

Strategy: validation

Validate before calling

if (aspectRatios.length == 0 || selectedByDefault < 0 || selectedByDefault >= aspectRatios.length) {
    throw new IllegalArgumentException("selectedByDefault must be in [0, " + (aspectRatios.length - 1) + "]");
}
options.setAspectRatioOptions(selectedByDefault, aspectRatios.toArray(new AspectRatio[0]));

Type guard

boolean isValidSelection(int selectedByDefault, AspectRatio[] options) {
    return options != null && options.length > 0 && selectedByDefault >= 0 && selectedByDefault < options.length;
}

Try / catch

try {
    options.setAspectRatioOptions(selectedByDefault, ratios);
} catch (IllegalArgumentException e) {
    options.setAspectRatioOptions(0, ratios);
}

Prevention

When it happens

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

Common situations: 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.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/e22abc3dc4b9459d. Report an issue: GitHub.