dotnet/reactive · error · ArgumentNullException

ArgumentNullException: source

Error message

ArgumentNullException: source

What it means

ObserveOn(source, CoreDispatcher) marshals observable notifications onto a UI thread's dispatcher; the 'source' observable must be non-null and ArgumentNullException('source') is thrown otherwise. The check runs synchronously before any scheduling occurs.

Solutions

  1. Ensure the observable is created before calling ObserveOn; use Observable.Defer to create it lazily.
  2. Fix the upstream factory that returned null instead of an observable.
  3. Subscribe on a fallback observable (e.g. Observable.Empty) when the stream is legitimately absent.

Example fix

// before
IObservable<EventData> stream = null;
var ui = stream.ObserveOn(Dispatcher);
// after
IObservable<EventData> stream = eventService.Streams; // must be non-null
var ui = stream.ObserveOn(Dispatcher);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new ArgumentNullException(nameof(source));
if (dispatcher is null) throw new ArgumentNullException(nameof(dispatcher));
var ui = source.ObserveOn(dispatcher);

Type guard

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

Try / catch

try
{
    var ui = source.ObserveOn(dispatcher);
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
    log.Warn("Stream was null before ObserveOn; check upstream initialization.");
}

Prevention

When it happens

Trigger: Calling nullSource.ObserveOn(dispatcher) in a UWP/WinRT app, typically when the observable came from a nullable field, a failed service resolution, or an optional event stream.

Common situations: ViewModels subscribing to streams that were only initialized on certain pages, or code that moved between WPF (DispatcherScheduler) and UWP (CoreDispatcher) and broke initialization.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive.WindowsRuntime/System.Reactive.Linq/CoreDispatcherObservable.cs:34

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