dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

Do(source, onNext) taps each element of the sequence with an action, and it validates the source sequence up front: a null IEnumerable<TSource> throws ArgumentNullException with parameter name 'source'. The check occurs at composition time, so you get the exception at the line that built the pipeline rather than during enumeration. This is part of the operator's fail-fast argument contract.

Solutions

  1. Coalesce with an empty sequence: (source ?? Enumerable.Empty<T>()).Do(onNext).
  2. Fix the producer to return Enumerable.Empty<T>() instead of null.
  3. Validate the source at your API boundary and throw a descriptive exception.
  4. Ensure the variable feeding the chain is initialized before pipeline construction.

Example fix

// before
var seq = GetItems(); // may return null
var logged = seq.Do(x => Console.WriteLine(x));
// after
var logged = (GetItems() ?? Enumerable.Empty<Item>()).Do(x => Console.WriteLine(x));
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) source = Enumerable.Empty<TSource>();
var result = source.Do(onNext);

Type guard

static bool IsSequence<TSource>(IEnumerable<TSource>? s) => s is not null;

Try / catch

try
{
    var result = source.Do(onNext);
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
    result = Enumerable.Empty<TSource>();
}

Prevention

When it happens

Trigger: Calling EnumerableEx.Do(null, onNext) or a chain like stream.Do(...) where stream is null — typically null return values from services, uninitialized fields, or nullable model properties.

Common situations: Logging/side-effect pipelines bolted onto a source that turned out to be null, repository methods returning null instead of empty, deserialized objects with null sequence properties, tests passing null.

Related errors


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

Appendix: source

Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/Do.cs:21

// See the LICENSE file in the project root for more information. 

using System.Collections.Generic;

namespace System.Linq
{
    public static partial class EnumerableEx
    {
        /// <summary>
        /// Lazily invokes an action for each value in the sequence.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="onNext">Action to invoke for each element.</param>
        /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
        public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (onNext == null)
                throw new ArgumentNullException(nameof(onNext));

            return DoCore(source, onNext, _ => { }, () => { });
        }

        /// <summary>
        /// Lazily invokes an action for each value in the sequence, and executes an action for successful termination.
        /// </summary>
        /// <typeparam name="TSource">Source sequence element type.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="onNext">Action to invoke for each element.</param>
        /// <param name="onCompleted">Action to invoke on successful termination of the sequence.</param>
        /// <returns>Sequence exhibiting the specified side-effects upon enumeration.</returns>
        public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

View on GitHub (pinned to 94b5d5ab91)