dotnet/reactive · error · ArgumentNullException

source

Error message

source

What it means

Experimental Expand(source, selector, scheduler) recursively re-applies selector to each produced value. It validates arguments synchronously and throws ArgumentNullException when the source observable is null. This fail-fast check mirrors the standard Rx operator contract.

Solutions

  1. Guarantee the source observable is non-null before Expand
  2. Substitute a default (Observable.Empty<TSource>()) when the source may legitimately be absent
  3. Trace where the null originated — often an earlier operator or a null-returning factory

Example fix

// before
var expanded = source.Expand(x => GetChildren(x), Scheduler.Default); // source null
// after
var expanded = (source ?? Observable.Empty<Node>()).Expand(x => GetChildren(x), Scheduler.Default);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) throw new ArgumentNullException(nameof(source), "Expand requires a non-null source observable.");

Type guard

bool IsValid<TSource>(IObservable<TSource> s) => s is not null;

Try / catch

try { var expanded = source.Expand(selector, scheduler); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { log.Error("Expand source was null", ex); }

Prevention

When it happens

Trigger: Calling Expand with a null IObservable<TSource>, usually from an unassigned field or a factory that returned null before the Expand call.

Common situations: DI-resolved streams that failed to bind, chaining after optional operators that can return null, pipeline construction where an earlier stage was skipped due to a flag.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/ObservableEx.cs:77

        #endregion

        #region Expand

        /// <summary>
        /// Expands an observable sequence by recursively invoking selector, using the specified scheduler to enumerate the queue of obtained sequences.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence and each of the recursively expanded sources obtained by running the selector function.</typeparam>
        /// <param name="source">Source sequence with the initial elements.</param>
        /// <param name="selector">Selector function to invoke for each produced element, resulting in another sequence to which the selector will be invoked recursively again.</param>
        /// <param name="scheduler">Scheduler on which to perform the expansion by enumerating the internal queue of obtained sequences.</param>
        /// <returns>An observable sequence containing all the elements produced by the recursive expansion.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> or <paramref name="scheduler"/> is null.</exception>
        [Experimental]
        public static IObservable<TSource> Expand<TSource>(this IObservable<TSource> source, Func<TSource, IObservable<TSource>> selector, IScheduler scheduler)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

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

            return s_impl.Expand(source, selector, scheduler);
        }

        /// <summary>
        /// Expands an observable sequence by recursively invoking selector.
        /// </summary>

View on GitHub (pinned to 94b5d5ab91)