dotnet/reactive · error · ArgumentNullException

iteratorMethod

Error message

iteratorMethod

What it means

ObservableEx.Create<TResult>(Func<IObserver<TResult>, IEnumerable<IObservable<object>>>) is an experimental factory that builds an observable from an iterator-style method. It validates its delegate eagerly and throws ArgumentNullException when iteratorMethod is null. Fail-fast keeps the null reference from surfacing only at Subscribe time.

Solutions

  1. Ensure the iteratorMethod Func is assigned before calling Create
  2. If the delegate is optional, branch to a non-experimental default observable (Observable.Empty or Observable.Defer) instead of passing null
  3. Check reflection or config-driven delegate construction for silent null results

Example fix

// before
Func<IObserver<int>, IEnumerable<IObservable<object>>> gen = GetGenerator(); // may be null
var obs = ObservableEx.Create(gen);
// after
var gen = GetGenerator();
var obs = gen != null ? ObservableEx.Create(gen) : Observable.Empty<int>();
Defensive patterns

Strategy: validation

Validate before calling

if (iteratorMethod == null) throw new ArgumentNullException(nameof(iteratorMethod), "Create requires a generator delegate.");

Type guard

bool IsValidGenerator<TResult>(Func<IObserver<TResult>, IEnumerable<IObservable<object>>> f) => f is not null;

Try / catch

try { var obs = ObservableEx.Create(iteratorMethod); }
catch (ArgumentNullException ex) { log.Error("Generator delegate was null", ex); }

Prevention

When it happens

Trigger: Passing a null Func to Create, typically a method-group or lambda variable that was never assigned, or a conditional factory returning null.

Common situations: Optional behavior hooks (logging/telemetry generators) that default to null, reflection-based lookups (GetMethod/Delegate.CreateDelegate) returning null, refactoring away a local function but leaving the null variable.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/ObservableEx.cs:36

#pragma warning disable IDE0044 // Make readonly: since 3rd party code reflects for this, we shouldn't pretend it won't change
        private static IQueryLanguageEx s_impl = QueryServices.GetQueryImpl<IQueryLanguageEx>(new QueryLanguageEx());
#pragma warning restore IDE1006, IDE0044 // Naming Styles, Make readonly

        #region Create

        /// <summary>
        /// Subscribes to each observable sequence returned by the iteratorMethod in sequence and returns the observable sequence of values sent to the observer given to the iteratorMethod.
        /// </summary>
        /// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
        /// <param name="iteratorMethod">Iterator method that produces elements in the resulting sequence by calling the given observer.</param>
        /// <returns>An observable sequence obtained by running the iterator and returning the elements that were sent to the observer.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="iteratorMethod"/> is null.</exception>
        [Experimental]
        public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, IEnumerable<IObservable<object>>> iteratorMethod)
        {
            if (iteratorMethod == null)
            {
                throw new ArgumentNullException(nameof(iteratorMethod));
            }

            return s_impl.Create(iteratorMethod);
        }

        /// <summary>
        /// Subscribes to each observable sequence returned by the iteratorMethod in sequence and produces a Unit value on the resulting sequence for each step of the iteration.
        /// </summary>
        /// <param name="iteratorMethod">Iterator method that drives the resulting observable sequence.</param>
        /// <returns>An observable sequence obtained by running the iterator and returning Unit values for each iteration step.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="iteratorMethod"/> is null.</exception>
        [Experimental]
        public static IObservable<Unit> Create(Func<IEnumerable<IObservable<object>>> iteratorMethod)
        {
            if (iteratorMethod == null)
            {
                throw new ArgumentNullException(nameof(iteratorMethod));
            }

View on GitHub (pinned to 94b5d5ab91)