dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'condition')

Error message

Value cannot be null. (Parameter 'condition')

What it means

The scheduler-overload of Observable.Generate throws ArgumentNullException when the 'condition' predicate is null. All delegates must be non-null because the scheduled generator loop invokes condition each step to decide whether to keep producing elements.

Solutions

  1. Pass a valid condition predicate like x => x < 10
  2. Default an absent predicate to _ => true
  3. Guard the argument before the call

Example fix

// before
Observable.Generate(0, null, i => i + 1, i => i, Scheduler.Default)
// after
Observable.Generate(0, x => x < 10, i => i + 1, i => i, Scheduler.Default)
Defensive patterns

Strategy: validation

Validate before calling

if (condition == null) throw new ArgumentNullException(nameof(condition));

Type guard

bool HasCondition(Func<TState,bool> c) => c is not null;

Try / catch

try { Observable.Generate(state, cond, it, sel, sched); } catch (ArgumentNullException ex) when (ex.ParamName == "condition") { /* supply default */ }

Prevention

When it happens

Trigger: Calling Observable.Generate<TState, TResult>(initialState, null, iterate, resultSelector, scheduler) — e.g. a predicate loaded from configuration that was absent.

Common situations: Scheduler-based generators built from dynamic rules engines; test harnesses parameterizing predicates where one parameter stayed null.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs:393

        }

        /// <summary>
        /// Generates an observable sequence by running a state-driven loop producing the sequence's elements, using the specified scheduler 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="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="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, 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 (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }

            return s_impl.Generate(initialState, condition, iterate, resultSelector, scheduler);

View on GitHub (pinned to 94b5d5ab91)