dotnet/reactive · error · ArgumentNullException

source

Error message

source

What it means

DispatcherObservable.ObserveOn (WPF) marshals observer callbacks onto the given WPF Dispatcher. It throws ArgumentNullException with parameter name 'source' when the observable sequence is null; a null sequence cannot be wrapped or subscribed.

Solutions

  1. Ensure the source IObservable<TSource> is non-null before calling ObserveOn.
  2. Make producers return Observable.Empty<T>() or Observable.Throw instead of null.
  3. Guard the composition with a null check or null-coalescing fallback.

Example fix

// before
_items = _repository.StreamItems(); // may be null
_items.ObserveOn(Dispatcher).Subscribe(Add);
// after
_items = _repository.StreamItems() ?? Observable.Empty<Item>();
_items.ObserveOn(Dispatcher).Subscribe(Add);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) source = Observable.Empty<TSource>();
if (dispatcher == null) throw new InvalidOperationException("Dispatcher unavailable");
source.ObserveOn(dispatcher).Subscribe(observer);

Type guard

static bool CanObserveOn<TSource>(IObservable<TSource> source, Dispatcher dispatcher) => source is not null && dispatcher is not null;

Try / catch

try { source.ObserveOn(dispatcher).Subscribe(OnNext); }
catch (ArgumentNullException ex) when (ex.ParamName is "source" or "dispatcher") { log.Error($"ObserveOn got null {ex.ParamName}"); }

Prevention

When it happens

Trigger: Calling nullSource.ObserveOn(dispatcher) — e.g. a service or repository method returned null instead of an IObservable, or an observable field was never assigned before composition.

Common situations: MVVM WPF apps where a ViewModel's data stream is null until initialization completes; DI container returning null for an unregistered binding; legacy code migrated from events where streams are optional.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Linq/DispatcherObservable.cs:30

    /// 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)