dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

ObserveOn(source, dispatcher) requires a non-null source sequence to schedule on the WPF Dispatcher. The library eagerly validates arguments and throws ArgumentNullException when source is null, since it cannot wrap a null observable. This is a defensive contract check before constructing the internal ObserveOn_ pipeline.

Solutions

  1. Ensure the IObservable passed as source is initialized before calling ObserveOn (use Observable.Empty or a ReplaySubject placeholder instead of null).
  2. Add a null check or assertion on the source sequence at the call site to fail early with a clearer message.
  3. Refactor producers to never return null observables (return Observable.Empty<T>() or Deferred observables).

Example fix

// before
IObservable<int> ticks = GetTicks(); // may return null
var onUi = ticks.ObserveOnDispatcher();
// after
IObservable<int> ticks = GetTicks() ?? Observable.Empty<int>();
var onUi = ticks.ObserveOn(dispatcher);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) throw new InvalidOperationException("source sequence must be initialized before ObserveOn");

Type guard

var safeSource = source ?? Observable.Empty<TSource>();

Try / catch

try { var ui = source.ObserveOn(dispatcher); } catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* fall back to Observable.Empty */ }

Prevention

When it happens

Trigger: Calling Observable.ObserveOn(mySequence, dispatcher) where mySequence is a null IObservable<TSource> reference (e.g., an uninitialized field, a method returning null, or a failed lookup).

Common situations: Storing an observable in a field/property that was never assigned; a factory method returning null instead of Observable.Empty; refactoring away the producer of a sequence while UI code still subscribes; MVVM bindings wired before the source stream is created.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive.Wpf/System.Reactive.Linq/DispatcherObservable.cs:34

    /// Provides a set of extension methods for scheduling actions performed through observable sequences on UI dispatchers.
    /// </summary>
    public static class DispatcherObservable
    {
        #region ObserveOn[Dispatcher]

        /// <summary>
        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="dispatcher">Dispatcher whose associated message loop is used to notify observers on.</param>
        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, Dispatcher dispatcher)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

            return ObserveOn_(source, dispatcher);
        }

        /// <summary>
        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="dispatcher">Dispatcher whose associated message loop is used to notify observers on.</param>
        /// <param name="priority">Priority to schedule work items at.</param>
        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>

View on GitHub (pinned to 94b5d5ab91)