dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'getEnumerator')

Error message

Value cannot be null. (Parameter 'getEnumerator')

What it means

System.Interactive's Create operator builds an IEnumerable from a factory delegate that produces an IEnumerator on each enumeration. The library eagerly validates its arguments and throws ArgumentNullException when the getEnumerator factory is null, so the failure surfaces at call time rather than at enumeration time.

Solutions

  1. Ensure the delegate passed to Create (or to Memoize/Publish/Share whose argument is forwarded) is non-null before calling
  2. Fix whatever produces the null factory — e.g. a factory dictionary or switch that returns null for the given key
  3. If the factory is conditionally unavailable, throw a more descriptive exception or fall back to an empty sequence
  4. Wrap the call in a null-coalescing expression: Create(() => ?? throw new InvalidOperationException(...))

Example fix

// before
var seq = EnumerableEx.Create(getEnumerator); // getEnumerator is null
// after
if (getEnumerator == null)
    throw new InvalidOperationException("No enumerator factory configured for this source.");
var seq = EnumerableEx.Create(getEnumerator);
Defensive patterns

Strategy: validation

Validate before calling

if (getEnumerator == null)
    throw new ArgumentNullException(nameof(getEnumerator));
// or defensively:
var factory = getEnumerator ?? throw new ArgumentException("Enumerator factory must be provided.");
var seq = EnumerableEx.Create(factory);

Type guard

static bool IsValidFactory<T>(Func<IEnumerator<T>> factory) => factory is not null;

Try / catch

try
{
    var seq = EnumerableEx.Create(getEnumerator);
}
catch (ArgumentNullException ex) when (ex.ParamName == "getEnumerator")
{
    // log and supply a default factory or rethrow with context
}

Prevention

When it happens

Trigger: Calling EnumerableEx.Create<TResult>(null) directly, or calling Memoize/Publish/Share with a null source or a null enumerator-factory argument that they forward to Create.

Common situations: A factory method returning null instead of a delegate (e.g. a switch statement that misses a default branch), a config-driven delegate lookup that resolves to null, or passing an uninitialized method-group variable.

Related errors


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

Appendix: source

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

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

using System.Collections;
using System.Collections.Generic;

namespace System.Linq
{
    public static partial class EnumerableEx
    {
        /// <summary>
        /// Creates an enumerable sequence based on an enumerator factory function.
        /// </summary>
        /// <typeparam name="TResult">Result sequence element type.</typeparam>
        /// <param name="getEnumerator">Enumerator factory function.</param>
        /// <returns>Sequence that will invoke the enumerator factory upon a call to GetEnumerator.</returns>
        public static IEnumerable<TResult> Create<TResult>(Func<IEnumerator<TResult>> getEnumerator)
        {
            if (getEnumerator == null)
                throw new ArgumentNullException(nameof(getEnumerator));

            return new AnonymousEnumerable<TResult>(getEnumerator);
        }

        /// <summary>
        /// Creates an enumerable sequence based on an asynchronous method that provides a yielder.
        /// </summary>
        /// <typeparam name="T">Result sequence element type.</typeparam>
        /// <param name="create">
        /// Delegate implementing an asynchronous method that can use the specified yielder to yield return
        /// values.
        /// </param>
        /// <returns>Sequence that will use the asynchronous method to obtain its elements.</returns>
        public static IEnumerable<T> Create<T>(Action<IYielder<T>> create)
        {
            if (create == null)
                throw new ArgumentNullException(nameof(create));

View on GitHub (pinned to 94b5d5ab91)