dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'scheduler')

Error message

Value cannot be null. (Parameter 'scheduler')

What it means

System.Reactive's public Take(source, count, scheduler) overload validates its arguments eagerly and synchronously. When the optional-behavior overload is called with a null IScheduler, it throws ArgumentNullException so scheduling configuration errors fail fast at composition time rather than inside the subscription pipeline. Rx operators intentionally never accept null arguments for scheduler/source/selector parameters.

Solutions

  1. Pass a concrete scheduler such as Scheduler.Default, Scheduler.CurrentThread, or Scheduler.Immediate.
  2. If the scheduler comes from DI or config, default it with `scheduler ?? Scheduler.Default` at the call site.
  3. If you do not need custom scheduling, call the Take(source, count) overload that needs no scheduler.

Example fix

// before
var result = source.Take(5, myScheduler); // myScheduler is null

// after
var result = source.Take(5, myScheduler ?? Scheduler.Default);
Defensive patterns

Strategy: validation

Validate before calling

if (scheduler is null) scheduler = Scheduler.Default; // before calling Take(source, count, scheduler)
ArgumentNullException.ThrowIfNull(source);

Type guard

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

Try / catch

try { var r = source.Take(count, scheduler); }
catch (ArgumentNullException ex) when (ex.ParamName == "scheduler") { /* fall back to Scheduler.Default */ }

Prevention

When it happens

Trigger: Calling Observable.Take(source, count, scheduler) with scheduler == null, e.g. Take(source, 5, (IScheduler)null), typically from a variable or DI-resolved scheduler that was never assigned.

Common situations: A Scheduler field/property defaulted to null instead of Scheduler.Default; a null returned from a config or factory lookup for the test scheduler; refactoring from the 2-arg Take overload to the timed Take(source, count, scheduler) overload while passing a placeholder null.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.StandardSequenceOperators.cs:1677

        /// <param name="scheduler">Scheduler used to produce an OnCompleted message in case <paramref name="count">count</paramref> is set to 0.</param>
        /// <returns>An observable sequence that contains the specified number of elements from the start of the input sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero.</exception>
        public static IObservable<TSource> Take<TSource>(this IObservable<TSource> source, int count, IScheduler scheduler)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

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

            return s_impl.Take(source, count, scheduler);
        }

        #endregion

        #region + TakeWhile +

        /// <summary>
        /// Returns elements from an observable sequence as long as a specified condition is true.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">A sequence to return elements from.</param>
        /// <param name="predicate">A function to test each element for a condition.</param>
        /// <returns>An observable sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
        public static IObservable<TSource> TakeWhile<TSource>(this IObservable<TSource> source, Func<TSource, bool> predicate)

View on GitHub (pinned to 94b5d5ab91)