dotnet/reactive · error · ArgumentNullException

source

Error message

source

What it means

ToEventPattern exposes an observable of WinRT event patterns as an object with a typed event; the source is validated because there would be no stream of (sender, args) pairs to forward to added handlers. Fix: pass a non-null IObservable<EventPattern<...>> from WindowsObservable.FromEventPattern.

Solutions

  1. Ensure the source observable is non-null before calling ToEventPattern
  2. Provide Observable.Empty<EventPattern<TSender,TEventArgs>>() as a non-null fallback
  3. Check upstream operator chains for null-producing factory methods
  4. Use nullable reference type annotations to catch this at compile time

Example fix

// before
IObservable<EventPattern<object, RoutedEventArgs>> obs = null;
var src = obs.ToEventPattern();
// after
var src = (obs ?? Observable.Empty<EventPattern<object, RoutedEventArgs>>()).ToEventPattern();
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) throw new ArgumentNullException(nameof(source));
var safe = source ?? Observable.Empty<EventPattern<TSender, TEventArgs>>();

Type guard

static IObservable<EventPattern<TS, TEA>> NonNullSource<TS, TEA>(IObservable<EventPattern<TS, TEA>> s) => s ?? Observable.Empty<EventPattern<TS, TEA>>();

Try / catch

try { var src = obs.ToEventPattern(); }
catch (ArgumentNullException ex) { Log($"{ex.ParamName} null when converting to event pattern"); }

Prevention

When it happens

Trigger: Calling observable.ToEventPattern<TSender,TEventArgs>() where the IObservable<EventPattern<TSender,TEventArgs>> is null, e.g. from an optional chain or an uninitialized field.

Common situations: Bridging Rx pipelines back to legacy event handlers where the observable was built conditionally; passing the result of a method that returns null on failure.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/WindowsObservable.Events.cs:111

                {
                    removeHandler(h);
                };
            });
        }

        /// <summary>
        /// Exposes an observable sequence as an object with a typed event.
        /// </summary>
        /// <typeparam name="TSender">The type of the sender that raises the event.</typeparam>
        /// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
        /// <param name="source">Observable source sequence.</param>
        /// <returns>The event source object.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        public static IEventPatternSource<TSender, TEventArgs> ToEventPattern<TSender, TEventArgs>(this IObservable<EventPattern<TSender, TEventArgs>> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return new EventPatternSource<TSender, TEventArgs>(source, static (h, evt) => h(evt.Sender!, evt.EventArgs));
        }
    }
}
#endif

View on GitHub (pinned to 94b5d5ab91)