dotnet/reactive · error · ArgumentNullException

throw new ArgumentNullException(nameof(resultSelector))

Error message

throw new ArgumentNullException(nameof(resultSelector))

What it means

The two-source ForkJoin overload throws ArgumentNullException when the resultSelector expression is null. A non-null Expression<Func<TSource1,TSource2,TResult>> is required because it is embedded in the MethodCallExpression sent to the IQbservable provider.

Solutions

  1. Pass a real lambda expression, e.g. (a, b) => new { a, b }
  2. Ensure any selector built dynamically is compiled/created before the call
  3. Null-check the selector variable before invoking ForkJoin

Example fix

// before
Expression<Func<int, int, int>> selector = null;
first.ForkJoin(second, selector);
// after
Expression<Func<int, int, int>> selector = (a, b) => a + b;
first.ForkJoin(second, selector);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

bool IsValidSelector<T1,T2,R>(Expression<Func<T1,T2,R>> s) => s is not null;

Try / catch

try { var q = first.ForkJoin(second, selector); } catch (ArgumentNullException ex) when (ex.ParamName == "resultSelector") { /* handle null selector */ }

Prevention

When it happens

Trigger: Calling first.ForkJoin(second, null), e.g. passing a null lambda variable or a null result from a selector factory.

Common situations: Conditionally built selector expressions that ended up null, or reflection-based code that failed to compile the lambda.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/QbservableEx.Homoicon.cs:1150

        /// </summary>
        /// <typeparam name="TSource1">The type of the elements in the first source sequence.</typeparam>
        /// <typeparam name="TSource2">The type of the elements in the second source sequence.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence, returned by the selector function.</typeparam>
        /// <param name="first">First observable sequence.</param>
        /// <param name="second">Second observable sequence.</param>
        /// <param name="resultSelector">Result selector function to invoke with the last elements of both sequences.</param>
        /// <returns>An observable sequence with the result of calling the selector function with the last elements of both input sequences.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="first" /> or <paramref name="second" /> or <paramref name="resultSelector" /> is null.</exception>
        [Experimental]
        public static IQbservable<TResult> ForkJoin<TSource1, TSource2, TResult>(this IQbservable<TSource1> first, IObservable<TSource2> second, Expression<Func<TSource1, TSource2, TResult>> resultSelector)
        {
            if (first == null)
                throw new ArgumentNullException(nameof(first));
            if (second == null)
                throw new ArgumentNullException(nameof(second));
            if (resultSelector == null)
                throw new ArgumentNullException(nameof(resultSelector));

            return first.Provider.CreateQuery<TResult>(
                Expression.Call(
                    null,
                    new Func<IQbservable<TSource1>, IObservable<TSource2>, Expression<Func<TSource1, TSource2, TResult>>, IQbservable<TResult>>(ForkJoin<TSource1, TSource2, TResult>).Method,
                    first.Expression,
                    GetSourceExpression(second),
                    resultSelector
                )
            );
        }
#endif

#if !STABLE
        /// <summary>
        /// Returns an observable sequence that is the result of invoking the selector on the source sequence, without sharing subscriptions.
        /// This operator allows for a fluent style of writing queries that use the same sequence multiple times.
        /// </summary>

View on GitHub (pinned to 94b5d5ab91)