dotnet/reactive · error · ArgumentNullException

dispatcherObject

Error message

dispatcherObject

What it means

Thrown by ObserveOn(source, DispatcherObject) when the DispatcherObject argument is null. The operator calls dispatcherObject.Dispatcher to find the scheduling target; a null object provides no dispatcher, so it throws ArgumentNullException('dispatcherObject').

Solutions

  1. Pass a live UI element (e.g. an initialized Window or Control)
  2. Use Application.Current.Dispatcher directly when the specific element is gone
  3. Check that the view was constructed (InitializeComponent called) before scheduling onto it

Example fix

// before
Window target = _window; // null after close
stream.ObserveOn(target);
// after
stream.ObserveOn(Application.Current.Dispatcher);
Defensive patterns

Strategy: validation

Validate before calling

var dispatcher = dispatcherObject?.Dispatcher ?? Application.Current?.Dispatcher ?? throw new InvalidOperationException("No dispatcher available");

Type guard

bool CanSchedule(DispatcherObject o) => o is not null && o.Dispatcher is not null;

Try / catch

try { stream.ObserveOn(view).Subscribe(...); }
catch (ArgumentNullException ex) when (ex.ParamName == "dispatcherObject") { stream.ObserveOn(Application.Current.Dispatcher).Subscribe(...); }

Prevention

When it happens

Trigger: Passing a null Window/Control/UserControl reference (e.g. a closed and cleared window reference, or a control accessed before InitializeComponent completed).

Common situations: Holding weak references to views that were garbage collected or set to null on navigation, and still using them to schedule UI updates from background observables.

Related errors


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

Appendix: source

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

        /// <summary>
        /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="dispatcherObject">Object to get the dispatcher from.</param>
        /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcherObject"/> is null.</exception>
        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

            return ObserveOn_(source, dispatcherObject.Dispatcher);
        }

        /// <summary>
        /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="dispatcherObject">Object to get the dispatcher from.</param>
        /// <param name="priority">Priority to schedule work items at.</param>
        /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcherObject"/> is null.</exception>
        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject, DispatcherPriority priority)
        {
            if (source == null)
            {

View on GitHub (pinned to 94b5d5ab91)