dotnet/reactive · error · ArgumentNullException

ArgumentNullException (default message: Value cannot be…

Error message

ArgumentNullException (default message: Value cannot be null. (Parameter 'scheduler'))

What it means

In the scheduler-based StartWith overload, the IScheduler argument must be non-null because it is used to emit the prepended values. A null scheduler throws ArgumentNullException with parameter name 'scheduler' immediately at the call site. Rx.NET validates this before any subscription occurs.

Solutions

  1. Pass a concrete scheduler such as Scheduler.Default, Scheduler.CurrentThread, or Scheduler.NewThread
  2. Inject the scheduler via the constructor instead of a nullable field
  3. Use the non-scheduler StartWith overload if no custom scheduling is needed

Example fix

// before
IScheduler sched = GetScheduler(); // may be null
var result = source.StartWith(sched, 1, 2);
// after
var result = source.StartWith(sched ?? Scheduler.Default, 1, 2);
Defensive patterns

Strategy: validation

Validate before calling

if (scheduler == null) throw new ArgumentNullException(nameof(scheduler));

Type guard

bool HasScheduler(IScheduler s) => s is not null;

Try / catch

try { var r = source.StartWith(scheduler, values); }
catch (ArgumentNullException ex) when (ex.ParamName == "scheduler") { /* fall back to default scheduler */ }

Prevention

When it happens

Trigger: Calling source.StartWith(null, values) where the IScheduler parameter is null.

Common situations: Uninjected scheduler dependency (DI container not registering IScheduler), a cached scheduler property not yet initialized, null returned from a scheduler factory.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Single.cs:844

        /// <summary>
        /// Prepends a sequence of values to an observable sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence to prepend values to.</param>
        /// <param name="scheduler">Scheduler to emit the prepended values on.</param>
        /// <param name="values">Values to prepend to the specified sequence.</param>
        /// <returns>The source sequence prepended with the specified values.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> or <paramref name="values"/> is null.</exception>
        public static IObservable<TSource> StartWith<TSource>(this IObservable<TSource> source, IScheduler scheduler, params TSource[] values)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

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

            return s_impl.StartWith(source, scheduler, values);
        }

        /// <summary>
        /// Prepends a sequence of values to an observable sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence to prepend values to.</param>
        /// <param name="scheduler">Scheduler to emit the prepended values on.</param>
        /// <param name="values">Values to prepend to the specified sequence.</param>
        /// <returns>The source sequence prepended with the specified values.</returns>

View on GitHub (pinned to 94b5d5ab91)