dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'dispatcherObject')
Error message
Value cannot be null. (Parameter 'dispatcherObject')
What it means
This ObserveOn overload validates the DispatcherObject parameter and throws ArgumentNullException('dispatcherObject') when null. The object's Dispatcher is accessed immediately (dispatcherObject.Dispatcher), so a null control/window reference fails fast with a clear parameter name.
Solutions
- Defer the subscription until the control/window exists: subscribe in Loaded/Initialized handlers instead of constructors.
- Use Application.Current.Dispatcher (or the Window's Dispatcher) as a stable target instead of a possibly-null control reference.
- Guard with a null check on the control and fall back to the current Dispatcher.
Example fix
// before var ui = source.ObserveOn(this.someControl); // someControl null pre-Load var ui2 = ui.Subscribe(x => ...); // after var dispatcher = this.someControl?.Dispatcher ?? Application.Current.Dispatcher; var ui = source.ObserveOn(dispatcher); var ui2 = ui.Subscribe(x => ...);
Defensive patterns
Strategy: validation
Validate before calling
if (dispatcherObject == null) dispatcherObject = Application.Current?.MainWindow; // plus fall back to dispatcher directly
Type guard
bool controlReady = dispatcherObject != null && dispatcherObject.Dispatcher != null;
Try / catch
try { var ui = source.ObserveOn(dispatcherObject); } catch (ArgumentNullException ex) when (ex.ParamName == "dispatcherObject") { var ui = source.ObserveOn(Application.Current.Dispatcher); } Prevention
- Subscribe in Loaded/OnApplyTemplate rather than constructors, so controls exist.
- Prefer passing a Dispatcher directly over a control reference when the control may be transient.
- Null-check controls obtained via FindName or GetTemplateChild before use.
When it happens
Trigger: Calling source.ObserveOn(control) where control is null: DataContext not yet bound, FindName/FindResource returning null, a window not yet loaded, or a control reference cleared after disposal.
Common situations: XAML name resolution timing (event handlers firing before Loaded); controls removed from the visual tree; templated parts not yet applied when a stream subscription is set up; code-behind running before InitializeComponent completes.
Related errors
- Value cannot be null. (Parameter 'dispatcher')
- Value cannot be null. (Parameter 'dispatcher')
- Value cannot be null. (Parameter 'dispatcher')
- Value cannot be null. (Parameter 'source')
- Value cannot be null. (Parameter 'scheduler')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/e4a053f42e4ede4c.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive.Wpf/System.Reactive.Linq/DispatcherObservable.cs:110
/// <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)