dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'action')

Error message

Value cannot be null. (Parameter 'action')

What it means

DispatcherScheduler.Schedule validates its action delegate before dispatching work to the WPF Dispatcher. A null action would fail later inside the dispatcher callback with no useful stack trace, so the library fails fast with ArgumentNullException. This is a caller programming error, not an environmental issue.

Solutions

  1. Ensure a non-null Func<IScheduler, TState, IDisposable> is passed to Schedule.
  2. Check that any delegate variable/field is initialized before the Schedule call.
  3. Guard at the call site: if (action != null) scheduler.Schedule(state, action);

Example fix

// before
scheduler.Schedule(state, null);
// after
IDisposable d = scheduler.Schedule(state, (sched, st) => { DoWork(st); return Disposable.Empty; });
Defensive patterns

Strategy: validation

Validate before calling

if (action == null) throw new InvalidOperationException("Schedule called before action was assigned");
var d = dispatcherScheduler.Schedule(state, action);

Type guard

static bool HasAction<TState>(Func<IScheduler, TState, IDisposable> action) => action is not null;

Try / catch

try { var d = scheduler.Schedule(state, action); }
catch (ArgumentNullException ex) when (ex.ParamName == "action") { log.Error("No action supplied to scheduler"); }

Prevention

When it happens

Trigger: Calling scheduler.Schedule(state, (sched, st) => ...) with a null delegate, e.g. var d = new DispatcherScheduler(d).Schedule(state, null); or passing a null delegate variable that was expected to be assigned.

Common situations: Building a wrapper that conditionally assigns the action delegate but leaves it null on some path; storing the action in a field initialized to null; refactoring code that used to pass a lambda into one that passes a nullable Func field.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Concurrency/DispatcherScheduler.cs:84

        /// <summary>
        /// Gets the priority at which work items will be dispatched.
        /// </summary>
        public System.Windows.Threading.DispatcherPriority Priority { get; }

        /// <summary>
        /// Schedules an action to be executed on the dispatcher.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="state">State passed to the action to be executed.</param>
        /// <param name="action">Action to be executed.</param>
        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
        public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var d = new SingleAssignmentDisposable();

            Dispatcher.BeginInvoke(
                new Action(() =>
                {
                    if (!d.IsDisposed)
                    {
                        d.Disposable = action(this, state);
                    }
                }),
                Priority
            );

            return d;
        }

View on GitHub (pinned to 94b5d5ab91)