dotnet/reactive · error · ArgumentNullException

throw new ArgumentNullException(nameof(condition));

Error message

throw new ArgumentNullException(nameof(condition));

What it means

Observable.Generate with a time selector validates every delegate; it throws ArgumentNullException when the condition predicate is null. Generate produces values from initialState while condition(state) holds, so the loop cannot run without a predicate.

Solutions

  1. Pass a real predicate, e.g. x => x < 10 (use _ => true for an effectively infinite sequence bounded elsewhere).
  2. If conditionality is intentional, supply x => true rather than null.
  3. Fix the predicate factory/dictionary lookup returning null.

Example fix

// before
var cond = rules.TryGetValue(name, out var c) ? c : null;
Observable.Generate(0, cond, x => x + 1, x => x, x => TimeSpan.FromSeconds(1));
// after
var cond = rules.TryGetValue(name, out var c) ? c : (Func<int, bool>)(_ => true);
Observable.Generate(0, cond, x => x + 1, x => x, x => TimeSpan.FromSeconds(1));
Defensive patterns

Strategy: validation

Validate before calling

if (condition == null) throw new InvalidOperationException("Generate requires a condition predicate");

Type guard

static Func<T,bool> RequirePredicate<T>(Func<T,bool> p) => p ?? throw new InvalidOperationException("predicate required");

Try / catch

try { Observable.Generate(state, condition, iterate, result, timeSel).Subscribe(...); }
catch (ArgumentNullException ex) { log.LogError("Generate missing delegate: {Param}", ex.ParamName); }

Prevention

When it happens

Trigger: Calling Generate(initialState, condition, iterate, resultSelector, timeSelector) with a null condition delegate — typically a dynamically chosen predicate that resolved to null.

Common situations: Predicate built from nullable config or a strategy pattern with an unregistered branch; also typos where condition was accidentally assigned from a void-returning setup method.

Related errors


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

Appendix: source

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

        #region + Generate +

        /// <summary>
        /// Generates an observable sequence by running a state-driven and temporal loop producing the sequence's elements.
        /// </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>
        /// <returns>The generated sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="condition"/> or <paramref name="iterate"/> or <paramref name="resultSelector"/> or <paramref name="timeSelector"/> 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, TimeSpan> timeSelector)
        {
            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));
            }

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

View on GitHub (pinned to 94b5d5ab91)