dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'scheduler')
Error message
Value cannot be null. (Parameter 'scheduler')
What it means
This ObserveOn overload requires a non-null DispatcherScheduler instance; it throws ArgumentNullException('scheduler') when null. The scheduler's Dispatcher and Priority are read immediately (scheduler.Dispatcher, scheduler.Priority), so a null wrapper cannot be tolerated.
Solutions
- Create the DispatcherScheduler explicitly from a valid Dispatcher: new DispatcherScheduler(Dispatcher.CurrentDispatcher).
- Verify the DI container registration provides a DispatcherScheduler and that resolution succeeded before calling ObserveOn.
- If the scheduler is optional in some hosts, branch: use the DispatcherScheduler overload only when available, otherwise a standard IScheduler overload.
Example fix
// before DispatcherScheduler sched = _schedulerProvider.Get(); // may return null var ui = source.ObserveOn(sched); // after var sched = _schedulerProvider.Get() ?? new DispatcherScheduler(Application.Current.Dispatcher); var ui = source.ObserveOn(sched);
Defensive patterns
Strategy: validation
Validate before calling
if (dispatcherScheduler == null) dispatcherScheduler = new DispatcherScheduler(Application.Current.Dispatcher);
Type guard
bool schedulerReady = dispatcherScheduler != null && dispatcherScheduler.Dispatcher != null;
Try / catch
try { var ui = source.ObserveOn(dispatcherScheduler); } catch (ArgumentNullException ex) when (ex.ParamName == "scheduler") { var ui = source.ObserveOn(Scheduler.Default); } Prevention
- Register DispatcherScheduler explicitly in the DI container.
- Create the scheduler on the UI thread so its Dispatcher is valid.
- Fall back to Dispatcher.CurrentDispatcher when no wrapper is configured.
When it happens
Trigger: Calling source.ObserveOn(dispatcherScheduler) where the DispatcherScheduler was never created or was injected/looked up as null, e.g., new DispatcherScheduler not yet assigned, or a container returning null for the scheduler registration.
Common situations: DI registration missing for DispatcherScheduler; creating the scheduler lazily on a background thread; accessing a static scheduler singleton before WPF application initialization.
Related errors
- throw new ArgumentNullException(nameof(scheduler));
- action (Value cannot be null)
- scheduler
- scheduler
- scheduler
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/90d18f112c910ece.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive.Wpf/System.Reactive.Linq/DispatcherObservable.cs:86
/// <summary>
/// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher scheduler.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Source sequence.</param>
/// <param name="scheduler">Dispatcher scheduler to notify observers on.</param>
/// <returns>The source sequence whose observations happen on the specified dispatcher scheduler.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>
public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DispatcherScheduler scheduler)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (scheduler == null)
{
throw new ArgumentNullException(nameof(scheduler));
}
return ObserveOn_(source, scheduler.Dispatcher, scheduler.Priority);
}
/// <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)
{View on GitHub (pinned to 94b5d5ab91)