dotnet/reactive · error · ArgumentNullException
ArgumentNullException: source
Error message
ArgumentNullException: source
What it means
ToEventPattern converts an IObservable of EventPattern back into a .NET-style event source, and it throws ArgumentNullException when the source observable is null. Because the entire conversion is driven by the source sequence, a null source cannot produce a meaningful result. The check is eager so the failure surfaces at the call site.
Solutions
- Ensure the observable passed to ToEventPattern is non-null before calling.
- If a producer can yield null, substitute Observable.Empty<EventPattern<TSender, TEventArgs>>() or the ?? operator.
- Move the ToEventPattern call inside the success branch where the source is known to exist.
Example fix
// before
IEventPatternSource<object, RoutedEventArgs> src = maybeObservable.ToEventPattern<object, RoutedEventArgs>(); // maybeObservable may be null
// after
IEventPatternSource<object, RoutedEventArgs> src = (maybeObservable ?? Observable.Empty<EventPattern<object, RoutedEventArgs>>())
.ToEventPattern<object, RoutedEventArgs>(); Defensive patterns
Strategy: validation
Validate before calling
if (source is null) source = Observable.Empty<EventPattern<TSender, TEventArgs>>();
Type guard
static bool IsObservableValid<TSender, TArgs>(IObservable<EventPattern<TSender, TArgs>>? s) => s is not null;
Try / catch
try { var src = observable.ToEventPattern<TSender, TEventArgs>(); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* use default event source */ } Prevention
- Adopt the convention of never returning null observables; return Observable.Empty instead.
- Apply the ?? operator with Observable.Empty when a source may be absent.
- Enable nullable reference types on the project.
When it happens
Trigger: Calling source.ToEventPattern<TSender, TEventArgs>() where source is a null IObservable<EventPattern<TSender, TEventArgs>>.
Common situations: A factory method or dictionary lookup returned a null observable; a conditional query pipeline short-circuited to null instead of Observable.Empty; refactoring changed a field to nullable without updating call sites.
Related errors
- ArgumentNullException: selector
- ArgumentNullException: source
- Value cannot be null. (Parameter 'asyncOperationSelector')
- Value cannot be null. (Parameter 'resultSelector')
- action
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/fdf7e4732c7fe76f.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive.WindowsRuntime/System.Reactive.Linq/WindowsObservable.Events.cs:114
{
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));
}
}
}
View on GitHub (pinned to 94b5d5ab91)