dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'resultSelector')

Error message

Value cannot be null. (Parameter 'resultSelector')

What it means

Observable.Generate throws ArgumentNullException ('resultSelector') when the selector mapping each state value to the emitted element is null. Rx validates arguments eagerly so the failure is attributed to the caller rather than surfacing as a NullReferenceException mid-sequence.

Solutions

  1. Supply a non-null result selector, e.g. state => state.ToString()
  2. If the projection is optional, use the identity function s => s
  3. Null-check the delegate before the call

Example fix

// before
Observable.Generate(0, c, i => i + 1, null)
// after
Observable.Generate(0, c, i => i + 1, i => i)
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

bool HasSelector(Func<TState,TResult> r) => r is not null;

Try / catch

try { Observable.Generate(state, cond, it, sel); } catch (ArgumentNullException ex) when (ex.ParamName == "resultSelector") { /* use identity */ }

Prevention

When it happens

Trigger: Calling Observable.Generate<TState, TResult>(initialState, condition, iterate, null), commonly when the projection delegate is built dynamically or read from a nullable field.

Common situations: Composing pipelines where the result mapping is configurable and defaults were never set; serialization/DI returning null lambdas; copy-paste refactors dropping the selector.

Related errors


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

Appendix: source

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

        /// <param name="iterate">Iteration step function.</param>
        /// <param name="resultSelector">Selector function for results produced in the sequence.</param>
        /// <returns>The generated sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="condition"/> or <paramref name="iterate"/> or <paramref name="resultSelector"/> is null.</exception>
        public static IObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
        {
            if (condition == null)
            {
                throw new ArgumentNullException(nameof(condition));
            }

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

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

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

        /// <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)

View on GitHub (pinned to 94b5d5ab91)