dotnet/reactive · error · ArgumentNullException

throw new ArgumentNullException(nameof(source));

Error message

throw new ArgumentNullException(nameof(source));

What it means

The Buffer(source, timeSpan, count, scheduler) overload throws ArgumentNullException when the source IObservable<TSource> is null. Like all Rx operators, Buffer requires a non-null source sequence and validates arguments eagerly before returning the operator. A null source is a caller bug, not a valid stream.

Solutions

  1. Guard the source before the call or substitute Observable.Empty<TSource>()
  2. Fix the upstream producer to return Observable.Empty instead of null
  3. Enable C# nullable reference types so the null path is caught at compile time

Example fix

// before
var buffered = GetStream()?.Buffer(TimeSpan.FromSeconds(1), 5, Scheduler.Default);
// after
var src = GetStream() ?? Observable.Empty<int>();
var buffered = src.Buffer(TimeSpan.FromSeconds(1), 5, Scheduler.Default);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new ArgumentNullException(nameof(source));
// or: source ??= Observable.Empty<TSource>();

Type guard

static bool HasSource<T>(IObservable<T>? s) => s is not null;

Try / catch

try { var b = source.Buffer(timeSpan, count, scheduler); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* fall back to Observable.Empty pipeline */ }

Prevention

When it happens

Trigger: Calling Observable.Buffer(null, timeSpan, count, scheduler) — commonly from an uninitialized observable field, a factory method returning null, or a conditional lookup that yields null instead of an empty observable.

Common situations: Chaining Rx operators off data retrieved from a cache/repository that returns null when missing; nullable reference annotations not enabled so null flows unchecked; refactors where source creation was moved behind a method that can fail silently.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs:231

        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence, and in the lists in the result sequence.</typeparam>
        /// <param name="source">Source sequence to produce buffers over.</param>
        /// <param name="timeSpan">Maximum time length of a buffer.</param>
        /// <param name="count">Maximum element count of a buffer.</param>
        /// <param name="scheduler">Scheduler to run buffering timers on.</param>
        /// <returns>An observable sequence of buffers.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="timeSpan"/> is less than TimeSpan.Zero. -or- <paramref name="count"/> is less than or equal to zero.</exception>
        /// <remarks>
        /// Specifying a TimeSpan.Zero value for <paramref name="timeSpan"/> is not recommended but supported, causing the scheduler to create buffers as fast as it can.
        /// Because all source sequence elements end up in one of the buffers, some buffers won't have a zero time span. This is a side-effect of the asynchrony introduced
        /// by the scheduler, where the action to close the current buffer and to create a new buffer may not execute immediately, despite the TimeSpan.Zero due time.
        /// </remarks>
        public static IObservable<IList<TSource>> Buffer<TSource>(this IObservable<TSource> source, TimeSpan timeSpan, int count, IScheduler scheduler)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (timeSpan < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(timeSpan));
            }

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

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

            return s_impl.Buffer(source, timeSpan, count, scheduler);

View on GitHub (pinned to 94b5d5ab91)