dotnet/reactive · error · ArgumentNullException

ArgumentNullException(nameof(second))

Error message

ArgumentNullException(nameof(second))

What it means

CombineLatest<TSource1,TSource2,TResult> throws ArgumentNullException when the second observable is null (Observable.Multiple.cs:269). The first argument was non-null (its check at line 264 passed), so validation proceeded to the second parameter. The throw is synchronous, before any subscription occurs.

Solutions

  1. Pass a real (possibly empty) observable instead of null: Observable.Empty<TSource2>().
  2. Coalesce before combining: (second ?? Observable.Empty<TSource2>()).
  3. Fix the source factory so it always returns an observable, never null.

Example fix

// before
var combined = clicks.CombineLatest(GetValueStream(), (c, v) => Format(c, v));
// GetValueStream() can return null
// after
var valueStream = GetValueStream() ?? Observable.Empty<Value>();
var combined = clicks.CombineLatest(valueStream, (c, v) => Format(c, v));
Defensive patterns

Strategy: validation

Validate before calling

if (second == null) throw new InvalidOperationException("second source required"); // or: second ??= Observable.Empty<TSource2>();

Type guard

bool IsStreamReady(IObservable<TSource2>? second) => second is not null;

Try / catch

try { var result = first.CombineLatest(second, selector); } catch (ArgumentNullException ex) when (ex.ParamName == "second") { /* substitute Observable.Empty<TSource2>() */ }

Prevention

When it happens

Trigger: Calling first.CombineLatest(null, selector) or Observable.CombineLatest(first, null, selector) with a null second IObservable.

Common situations: Second stream comes from an optional feature toggle or cache lookup that returned null; a Subject field not yet constructed; API migration where an optional source was passed as null instead of Observable.Empty.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Multiple.cs:269

        /// <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 source.</param>
        /// <param name="second">Second observable source.</param>
        /// <param name="resultSelector">Function to invoke whenever either of the sources produces an element.</param>
        /// <returns>An observable sequence containing the result of combining elements of both sources using the specified result selector function.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> or <paramref name="resultSelector"/> is null.</exception>
        /// <remarks>If a non-empty source completes, its very last value will be used for creating subsequent combinations until all sources terminate.</remarks>
        public static IObservable<TResult> CombineLatest<TSource1, TSource2, TResult>(this IObservable<TSource1> first, IObservable<TSource2> second, 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 s_impl.CombineLatest(first, second, resultSelector);
        }

        /// <summary>
        /// Merges the specified observable sequences into one observable sequence by using the selector function whenever any of the observable sequences produces an element.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence, returned by the selector function.</typeparam>
        /// <param name="sources">Observable sources.</param>
        /// <param name="resultSelector">Function to invoke whenever any of the sources produces an element. For efficiency, the input list is reused after the selector returns. Either aggregate or copy the values during the function call.</param>
        /// <returns>An observable sequence containing the result of combining elements of the sources using the specified result selector function.</returns>

View on GitHub (pinned to 94b5d5ab91)