dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

The UWP ObserveOn extension requires a non-null source observable; passing null throws ArgumentNullException('source'). Rx guards argument nulls eagerly at the operator call site so the failure happens immediately rather than inside the subscription pipeline.

Solutions

  1. Check why the source observable is null before calling ObserveOn; initialize or assign it (e.g. Observable.Empty<TSource>() as a safe default).
  2. Add a null/has-value check on the variable producing the source sequence.
  3. If the source comes from an event or property, ensure the publisher actually assigned the observable before the UI subscription runs.

Example fix

// before
IObservable<int> source = GetSource(); // may return null
var obs = source.ObserveOn(myControl);
// after
var src = GetSource() ?? Observable.Empty<int>();
var obs = src.ObserveOn(myControl);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) source = Observable.Empty<TSource>();
var obs = source.ObserveOn(dependencyObject);

Type guard

static IObservable<T> NotNull<T>(IObservable<T> s) => s ?? Observable.Empty<T>();

Try / catch

try
{
    var obs = source.ObserveOn(dependencyObject);
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
    // fall back to empty/default sequence
}

Prevention

When it happens

Trigger: Calling source.ObserveOn(dependencyObject) (System.Reactive.Uwp DependencyObjectObservable, line 34) where the IObservable<TSource> argument is null.

Common situations: A factory method or property returning null observable, a nullable field not yet initialized, or a method chain where an earlier operator returned null instead of an empty sequence.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive.Uwp/System.Reactive.Linq/DependencyObjectObservable.cs:34

    /// <summary>
    /// Rx extension methods for UWP's (Windows.UI.Xaml) <see cref="DependencyObject"/>.
    /// </summary>
    [CLSCompliant(false)]
    public static class DependencyObjectObservable
    {
        /// <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="dependencyObject">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="dependencyObject"/> is null.</exception>
        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

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

View on GitHub (pinned to 94b5d5ab91)