quarkusio/quarkus · error · IllegalArgumentException

Last index must be >= start index: is not larger or equal t

Error message

Last index must be >= start index:  is not larger or equal to 

What it means

Range requires lastIndex >= startIndex because an inclusive 0-based range whose end precedes its start is invalid. The constructor throws IllegalArgumentException embedding both offending values in the message.

Source

Thrown at extensions/panache/panache-common/runtime/src/main/java/io/quarkus/panache/common/Range.java:35

 */
public class Range {
    // FIXME: bump to long? Jakarta Data uses longs for its Limit type
    private final int startIndex;
    private final int lastIndex;

    /**
     * Creates a new range
     *
     * @param startIndex the start index to include, 0-based
     * @param lastIndex the end index to include, 0-based
     */
    public Range(int startIndex, int lastIndex) {
        if (startIndex < 0)
            throw new IllegalArgumentException("Start index must be >= 0: " + startIndex);
        if (lastIndex < 0)
            throw new IllegalArgumentException("Last index must be >= 0: " + lastIndex);
        if (lastIndex < startIndex)
            throw new IllegalArgumentException(
                    "Last index must be >= start index: " + lastIndex + " is not larger or equal to " + startIndex);
        this.startIndex = startIndex;
        this.lastIndex = lastIndex;
    }

    /**
     * Creates a new range
     *
     * @param startIndex the start index to include, 0-based
     * @param lastIndex the end index to include, 0-based
     */
    public static Range of(int startIndex, int lastIndex) {
        return new Range(startIndex, lastIndex);
    }

    /**
     * @return the start index to include, 0-based
     */

View on GitHub (pinned to e1c734241f)

Solutions

  1. Validate before constructing: if (last < start) swap them or return an empty result.
  2. Clamp both ends: start=Math.max(0,start); end=Math.max(start,end).
  3. Check argument order at call sites of Range.of.

Example fix

// before
Range r = Range.of(start, end); // end may be < start
// after
int s = Math.max(0, start);
Range r = Range.of(s, Math.max(s, end));
Defensive patterns

Strategy: validation

Validate before calling

if (lastIndex < startIndex) {
    throw new BadRequestException("lastIndex must be >= startIndex");
}
Range r = Range.of(startIndex, lastIndex);

Type guard

Range orderedRange(int a, int b) {
    int start = Math.max(0, Math.min(a, b));
    int last  = Math.max(start, Math.max(a, b));
    return Range.of(start, last);
}

Try / catch

try {
    Range r = Range.of(startIndex, lastIndex);
} catch (IllegalArgumentException e) {
    throw new BadRequestException("Invalid range: " + e.getMessage());
}

Prevention

When it happens

Trigger: Calling new Range(5, 2) or Range.of where the computed end is smaller than the start — commonly from reversed offsets, swapped arguments, or negative-count math.

Common situations: Argument order confusion (passing end, start instead of start, end); slicing with start beyond the data length; count math yielding end < start after clamping only one bound.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/dc355a7391103753. Report an issue: GitHub.