ReactiveX/RxJava · error · IllegalArgumentException

count >= 0 required but it was {}

Error message

count >= 0 required but it was {}

What it means

Thrown by Streamable.intervalRange (scheduler overload) when count is negative. The operator emits count longs starting at start; negative counts are invalid and rejected at assembly time. Note this overload validates unit/scheduler for null first, then count.

Source

Thrown at src/main/java/io/reactivex/rxjava4/core/Streamable.java:405

     * numbers from {@code start} up to {@code start + count} exclusive with the given period.
     * <p>
     * If there are processing delays, this source may emit multiple queued up items in a quick succession.
     * @param start the first long value to emit
     * @param count the number of items to emit, use {@link Long#MAX_VALUE} for an unlimited range
     * @param initialDelay the time to delay before the {@code start} item is emitted
     * @param period the period of how often emit the next item
     * @param unit the time unit for both {@code initialDelay} and {@code period}
     * @param scheduler the scheduler to use for the timed waiting
     * @return the new {@code Streamable} instance
     * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null}
     * @throws IllegalArgumentException if {@code count} is negative
     */
    static Streamable<Long> intervalRange(long start, long count,
            long initialDelay, long period, TimeUnit unit, Scheduler scheduler) {
        Objects.requireNonNull(unit, "unit is null");
        Objects.requireNonNull(scheduler, "scheduler is null");
        if (count < 0) {
            throw new IllegalArgumentException("count >= 0 required but it was " + count);
        }

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

        return RxJavaPlugins.onAssembly(new StreamableIntervalRange(start, count, initialDelay, period, unit, scheduler, null));
    }

    /**
     * Constructs a {@code Streamable} that after the initial delay, starts emitting an ever increasing
     * numbers from {@code start} up to {@code start + count} exclusive with the given period.
     * <p>
     * If the provided {@link ExecutorService} is a {@link ScheduledExecutorService}, its
     * {@link ScheduledExecutorService#scheduleAtFixedRate(Runnable, long, long, TimeUnit)} will be used.
     * Otherwise, a plain {@code ExecutorService} will be wrapped via {@link Schedulers#from(Executor)}.
     * <p>

View on GitHub (pinned to a8ab535614)

Solutions

  1. Bounds-check count before calling: if (count < 0L) handle or clamp to 0L.
  2. Fix the upstream subtraction producing the negative value.
  3. Return Streamable.empty() for count <= 0 instead of calling intervalRange.

Example fix

// before
long count = quota - used; // can be negative
Streamable.intervalRange(0, count, 0, 1, TimeUnit.SECONDS, scheduler);

// after
long count = Math.max(0L, quota - used);
Streamable<Long> s = count == 0L
    ? Streamable.empty()
    : Streamable.intervalRange(0, count, 0, 1, TimeUnit.SECONDS, scheduler);
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0L) {
    return Streamable.empty();
}

Try / catch

try {
    return Streamable.intervalRange(start, count, initialDelay, period, unit, scheduler);
} catch (IllegalArgumentException e) {
    return Streamable.empty();
}

Prevention

When it happens

Trigger: Calling Streamable.intervalRange(start, count, initialDelay, period, unit, scheduler) with count < 0. Typical when count is computed or config-driven and can underflow.

Common situations: Config limits defaulting to -1; arithmetic like available - used that goes negative; untrusted inputs not bounds-checked.

Related errors


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