dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'selector')

Error message

Value cannot be null. (Parameter 'selector')

What it means

Select requires a non-null selector function and throws ArgumentNullException (parameter 'selector') when the projection delegate is null. The guard is evaluated eagerly at pipeline composition time, pointing the failure at the caller. The XML docs list this as part of Select's contract.

Solutions

  1. Supply a non-null lambda: source.Select(x => x.Transform())
  2. Default the delegate: selector ?? (x => x) for identity projection
  3. Fix the resolver/factory that produced a null mapper

Example fix

// before
Func<string, int> mapper = null;
var query = source.Select(mapper);
// after
var query = source.Select(x => x.Length);
Defensive patterns

Strategy: validation

Validate before calling

Func<TSource, TResult> safeSel = selector ?? (x => x);
var projected = source.Select(safeSel);

Type guard

static bool HasSelector<T,R>(Func<T,R>? f) => f is not null;

Try / catch

try { var projected = source.Select(selector); }
catch (ArgumentNullException ex) when (ex.ParamName == "selector")
{ /* fall back to identity projection or log config bug */ }

Prevention

When it happens

Trigger: Calling source.Select(null) or Observable.Select(source, null) — passing a null Func<TSource, TResult>, often from an unassigned delegate variable, reflection-resolved method, or a refactor that removed the lambda.

Common situations: Storing projections in fields/config that were never initialized; dynamically composed pipelines where the mapper name failed to resolve; copy-paste that dropped the transform.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs:964

        /// <summary>
        /// Projects each element of an observable sequence into a new form.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence, obtained by running the selector function for each element in the source sequence.</typeparam>
        /// <param name="source">A sequence of elements to invoke a transform function on.</param>
        /// <param name="selector">A transform function to apply to each source element.</param>
        /// <returns>An observable sequence whose elements are the result of invoking the transform function on each element of source.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
        public static IObservable<TResult> Select<TSource, TResult>(this IObservable<TSource> source, Func<TSource, TResult> selector)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

            return s_impl.Select(source, selector);
        }

        /// <summary>
        /// Projects each element of an observable sequence into a new form by incorporating the element's index.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence, obtained by running the selector function for each element in the source sequence.</typeparam>
        /// <param name="source">A sequence of elements to invoke a transform function on.</param>
        /// <param name="selector">A transform function to apply to each source element; the second parameter of the function represents the index of the source element.</param>
        /// <returns>An observable sequence whose elements are the result of invoking the transform function on each element of source.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
        public static IObservable<TResult> Select<TSource, TResult>(this IObservable<TSource> source, Func<TSource, int, TResult> selector)
        {
            if (source == null)
            {

View on GitHub (pinned to 94b5d5ab91)