dotnet/reactive · error · ArgumentNullException

condition

Error message

condition

What it means

The scheduler-based timed Observable.Generate overload throws ArgumentNullException when the condition predicate (Func<TState, bool>) is null. condition determines when generation stops; without it the loop cannot be evaluated. Rx validates this eagerly at call time, before returning the observable.

Solutions

  1. Pass an explicit stop predicate, e.g. state => state < 10.
  2. If termination should never occur, use _ => true (with an external disposal mechanism) rather than null.
  3. Guard with ArgumentNullException.ThrowIfNull(condition) at your own API boundary before forwarding to Generate.

Example fix

// before
var xs = Observable.Generate(0, null, i => i + 1, i => i, i => TimeSpan.FromSeconds(1), Scheduler.Default);
// after
var xs = Observable.Generate(0, i => i < 10, i => i + 1, i => i, i => TimeSpan.FromSeconds(1), Scheduler.Default);
Defensive patterns

Strategy: validation

Validate before calling

if (condition == null)
    throw new ArgumentNullException(nameof(condition));
var xs = Observable.Generate(initialState, condition, iterate, resultSelector, timeSelector, scheduler);

Type guard

bool HasCondition<TState>(Func<TState, bool> condition) => condition != null;

Try / catch

try
{
    var xs = Observable.Generate(state, cond, iter, sel, timeSel, scheduler);
}
catch (ArgumentNullException ex) when (ex.ParamName == "condition")
{
    logger.LogError(ex, "Generate condition predicate was null");
}

Prevention

When it happens

Trigger: Calling Observable.Generate<TState, TResult>(initialState, condition, iterate, resultSelector, timeSelector, scheduler) with condition == null, e.g. when the predicate is assembled dynamically, loaded from configuration, or omitted because a previous overload signature lacked it.

Common situations: Composing queries where the stop predicate is optional and passed straight through; refactoring from Reactive Extensions v2 style code where different Generate overloads existed; copy-paste between overloads dropping one argument.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/2255588f495816c0. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs:745

        /// <summary>
        /// Generates an observable sequence by running a state-driven and temporal loop producing the sequence's elements, using the specified scheduler to run timers and to send out observer messages.
        /// </summary>
        /// <typeparam name="TState">The type of the state used in the generator loop.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
        /// <param name="initialState">Initial state.</param>
        /// <param name="condition">Condition to terminate generation (upon returning false).</param>
        /// <param name="iterate">Iteration step function.</param>
        /// <param name="resultSelector">Selector function for results produced in the sequence.</param>
        /// <param name="timeSelector">Time selector function to control the speed of values being produced each iteration.</param>
        /// <param name="scheduler">Scheduler on which to run the generator loop.</param>
        /// <returns>The generated sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="condition"/> or <paramref name="iterate"/> or <paramref name="resultSelector"/> or <paramref name="timeSelector"/> or <paramref name="scheduler"/> is null.</exception>
        public static IObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, Func<TState, DateTimeOffset> timeSelector, IScheduler scheduler)
        {
            if (condition == null)
            {
                throw new ArgumentNullException(nameof(condition));
            }

            if (iterate == null)
            {
                throw new ArgumentNullException(nameof(iterate));
            }

            if (resultSelector == null)
            {
                throw new ArgumentNullException(nameof(resultSelector));
            }

            if (timeSelector == null)
            {
                throw new ArgumentNullException(nameof(timeSelector));
            }

            if (scheduler == null)

View on GitHub (pinned to 94b5d5ab91)