dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'function')

Error message

Value cannot be null. (Parameter 'function')

What it means

Observable.Start<TResult>(Func<TResult>) invokes the given function synchronously-or-on-schedule and publishes its result as an observable. Rx validates the function eagerly and throws ArgumentNullException with Parameter 'function' when null, since there is no computation to run.

Solutions

  1. Pass a valid non-null Func<TResult> to Observable.Start.
  2. Ensure any delegate field/dependency is initialized before calling Start.
  3. If the computation may be absent, fall back to Observable.Empty<TResult>() or a default result instead of Start(null).

Example fix

// before
Func<int> compute = config.Computations["fast"] ?? null;
Observable.Start(compute);
// after
Func<int> compute = config.Computations["fast"];
Observable.Start(() => compute != null ? compute() : 0);
Defensive patterns

Strategy: validation

Validate before calling

if (function is null) throw new ArgumentNullException(nameof(function));
var observable = Observable.Start(function);

Type guard

static bool IsValidFunction<TResult>(Func<TResult> function) => function is not null;

Try / catch

try { var obs = Observable.Start(function); }
catch (ArgumentNullException ex) when (ex.ParamName == "function") { obs = Observable.Empty<TResult>(); }

Prevention

When it happens

Trigger: Calling Observable.Start(function) with a null Func<TResult> (line 991 in Observable.Async.cs), e.g. a null delegate field, an unassigned lambda variable, or a factory that returned null.

Common situations: Dependency-injected computation delegate not registered; conditional logic skipped assignment; passing the result of a lookup (e.g. dictionary of delegates) that missed.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Async.cs:991

        /// <summary>
        /// Invokes the specified function asynchronously, surfacing the result through an observable sequence.
        /// </summary>
        /// <typeparam name="TResult">The type of the result returned by the function.</typeparam>
        /// <param name="function">Function to run asynchronously.</param>
        /// <returns>An observable sequence exposing the function's result value, or an exception.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="function"/> is null.</exception>
        /// <remarks>
        /// <list type="bullet">
        /// <item><description>The function is called immediately, not during the subscription of the resulting sequence.</description></item>
        /// <item><description>Multiple subscriptions to the resulting sequence can observe the function's result.</description></item>
        /// </list>
        /// </remarks>
        public static IObservable<TResult> Start<TResult>(Func<TResult> function)
        {
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function));
            }

            return s_impl.Start(function);
        }

        /// <summary>
        /// Invokes the specified function asynchronously on the specified scheduler, surfacing the result through an observable sequence
        /// </summary>
        /// <typeparam name="TResult">The type of the result returned by the function.</typeparam>
        /// <param name="function">Function to run asynchronously.</param>
        /// <param name="scheduler">Scheduler to run the function on.</param>
        /// <returns>An observable sequence exposing the function's result value, or an exception.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="function"/> or <paramref name="scheduler"/> is null.</exception>
        /// <remarks>
        /// <list type="bullet">
        /// <item><description>The function is called immediately, not during the subscription of the resulting sequence.</description></item>
        /// <item><description>Multiple subscriptions to the resulting sequence can observe the function's result.</description></item>
        /// </list>

View on GitHub (pinned to 94b5d5ab91)