dotnet/reactive · error · ArgumentNullException

Thrown when resultSelector is null (ArgumentNullException…

Error message

Thrown when resultSelector is null (ArgumentNullException, param name: resultSelector)

What it means

Generate uses resultSelector to compute each element from the current state. A null selector cannot produce elements, so the library throws ArgumentNullException with param name 'resultSelector' before returning the sequence.

Solutions

  1. Supply a concrete result selector, e.g. state => state.ToString().
  2. Coalesce to a default projection: resultSelector ?? (s => default(TResult)).
  3. Fix the factory/resolver that should have produced the selector.

Example fix

// before
var seq = GeneratorEx.Generate(0, c, it, sel); // sel is null
// after
var seq = GeneratorEx.Generate(0, c, it, sel ?? (s => default(int)));
Defensive patterns

Strategy: validation

Validate before calling

if (resultSelector is null) throw new ArgumentException("resultSelector is required");

Type guard

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

Try / catch

try { var seq = GeneratorEx.Generate(state, condition, iterate, resultSelector); }
catch (ArgumentNullException ex) when (ex.ParamName == "resultSelector") { log.Error("result selector missing"); }

Prevention

When it happens

Trigger: Calling Generate(initialState, condition, iterate, null) — e.g. a projection delegate obtained from a nullable lookup or an unassigned mapping in a pipeline builder.

Common situations: Mapping functions supplied via options objects or DI; code-generation/reflection-based selector resolution failing; copy-paste refactors dropping the lambda argument.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/Generate.cs:28

    {
        /// <summary>
        /// Generates a sequence by mimicking a for loop.
        /// </summary>
        /// <typeparam name="TState">State type.</typeparam>
        /// <typeparam name="TResult">Result sequence element type.</typeparam>
        /// <param name="initialState">Initial state of the generator loop.</param>
        /// <param name="condition">Loop condition.</param>
        /// <param name="iterate">State update function to run after every iteration of the generator loop.</param>
        /// <param name="resultSelector">Result selector to compute resulting sequence elements.</param>
        /// <returns>Sequence obtained by running the generator loop, yielding computed elements.</returns>
        public static IEnumerable<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 GenerateCore(initialState, condition, iterate, resultSelector);
        }

        private static IEnumerable<TResult> GenerateCore<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
        {
            for (var i = initialState; condition(i); i = iterate(i))
            {
                yield return resultSelector(i);
            }
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)