dotnet/reactive · error · ArgumentNullException

source (Parameter 'source')

Error message

source (Parameter 'source')

What it means

The ObserveOn(CoreDispatcher) extension marshals observable notifications onto a WinRT CoreDispatcher; the source observable must be non-null. ArgumentNullException with ParamName 'source' is thrown by the first guard in the method. Without it, Synchronization.ObserveOn would fail later or produce a NullReferenceException on subscription.

Solutions

  1. Verify the receiver observable is initialized before calling ObserveOn; use Observable.Empty<T>() or a Subject<T> instead of null.
  2. Reorder initialization so the stream is created before UI marshalling is set up.
  3. Wrap with a null-coalescing default: (source ?? Observable.Empty<T>()).ObserveOn(dispatcher).

Example fix

// before
var bound = vm.Stream.ObserveOn(dispatcher).Subscribe(OnNext); // vm.Stream may be null
// after
var bound = (vm.Stream ?? Observable.Empty<Item>()).ObserveOn(dispatcher).Subscribe(OnNext);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null || dispatcher == null)
    throw new InvalidOperationException("ObserveOn requires a non-null source and dispatcher");

Type guard

bool CanObserveOn<TSource>(IObservable<TSource> s, CoreDispatcher d) => s is not null && d is not null;

Try / catch

try { subscription = stream.ObserveOn(dispatcher).Subscribe(onNext); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { subscription = Subscription.Empty; }

Prevention

When it happens

Trigger: Calling someObservable.ObserveOn(dispatcher) where the receiver is null, e.g. an event stream property not yet initialized, or a service returning null instead of an empty sequence.

Common situations: Wiring UI bindings before the ViewModel's stream is created; timer/event-based streams that are null until started; unit tests forgetting to stub the observable.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/CoreDispatcherObservable.cs:35

    /// </summary>
    [CLSCompliant(false)]
    public static class CoreDispatcherObservable
    {
        #region ObserveOn[CoreDispatcher]

        /// <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, CoreDispatcher dispatcher)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

            return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(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)