ReactiveX/RxJava · error · IllegalArgumentException

count >= 0 required but it was

Error message

count >= 0 required but it was {}

What it means

Thrown by Flowable.intervalRange when the count argument is negative. intervalRange emits count longs starting at start; a negative count is meaningless and is rejected eagerly before any scheduling begins. The message interpolates the offending value so the caller can see exactly what was passed.

Solutions

  1. Bounds-check count before the call: if (count < 0L) handle the error or clamp to 0L.
  2. Fix the upstream computation producing the negative value (e.g., guard list.size() - offset).
  3. Default missing/unknown config to 0L rather than -1 so intervalRange returns an empty Flowable.

Example fix

// before
long count = pageSize - offset; // can be negative
Flowable.intervalRange(0, count, 0, 1, TimeUnit.SECONDS, scheduler);

// after
long count = pageSize - offset;
Flowable<Long> f = count < 0L
    ? Flowable.empty()
    : Flowable.intervalRange(0, count, 0, 1, TimeUnit.SECONDS, scheduler);
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0L) {
    // avoid Flowable.intervalRange throwing
    return Flowable.empty();
}

Try / catch

try {
    return Flowable.intervalRange(start, count, initialDelay, period, unit, scheduler);
} catch (IllegalArgumentException e) {
    // log and degrade to empty rather than crash
    return Flowable.empty();
}

Prevention

When it happens

Trigger: Calling Flowable.intervalRange(start, count, ...) with count < 0L. Typically the count comes from a config value, a computed size, or an off-by-one subtraction that yields -1.

Common situations: Config-driven paging where limit/pageSize is unset and defaults to -1; size calculations like list.size() - threshold that go negative; deserialized values that were never bounds-checked.

Related errors


AI-assisted analysis of ReactiveX/RxJava@a8ab535614 (2026-08-13). Data as JSON: /api/errors/e6ef7d3628095c15. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/reactivex/rxjava4/core/Flowable.java:2403

     * @param start that start value of the range
     * @param count the number of values to emit in total, if zero, the operator emits an {@code onComplete} after the initial delay.
     * @param initialDelay the initial delay before signaling the first value (the start)
     * @param period the period between subsequent values
     * @param unit the unit of measure of the {@code initialDelay} and {@code period} amounts
     * @param scheduler the target {@code Scheduler} where the values and terminal signals will be emitted
     * @return the new {@code Flowable} instance
     * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null}
     * @throws IllegalArgumentException
     *             if {@code count} is less than zero, or if {@code start} + {@code count} &minus; 1 exceeds
     *             {@link Long#MAX_VALUE}
     */
    @CheckReturnValue
    @NonNull
    @BackpressureSupport(BackpressureKind.ERROR)
    @SchedulerSupport(SchedulerSupport.CUSTOM)
    public static Flowable<Long> intervalRange(long start, long count, long initialDelay, long period, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) {
        if (count < 0L) {
            throw new IllegalArgumentException("count >= 0 required but it was " + count);
        }
        if (count == 0L) {
            return Flowable.<Long>empty().delay(initialDelay, unit, scheduler);
        }

        long end = start + (count - 1);
        if (start > 0 && end < 0) {
            throw new IllegalArgumentException("Overflow! start + count is bigger than Long.MAX_VALUE");
        }
        Objects.requireNonNull(unit, "unit is null");
        Objects.requireNonNull(scheduler, "scheduler is null");

        return RxJavaPlugins.onAssembly(new FlowableIntervalRange(start, end, Math.max(0L, initialDelay), Math.max(0L, period), unit, scheduler));
    }

    /**
     * Returns a {@code Flowable} that signals the given (constant reference) item and then completes.
     * <p>

View on GitHub (pinned to a8ab535614)