dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'source')
Error message
Value cannot be null. (Parameter 'source')
What it means
The protected EventPatternSourceBase constructor requires a non-null source sequence. Passing null throws ArgumentNullException for 'source'. This base class backs FromEventPattern-style bridges that expose an observable as a .NET event, so a source is mandatory.
Solutions
- Pass a valid non-null observable, e.g. Observable.FromEventPattern<...>(...) result.
- Null-check or ?? Observable.Empty<EventPattern<TSender,TEventArgs>>() the value before calling the base constructor.
Example fix
// before base(source, invokeHandler) // after base(source ?? Observable.Empty<EventPattern<TSender, TEventArgs>>(), invokeHandler)
Defensive patterns
Strategy: validation
Validate before calling
if (source == null) throw new ArgumentNullException(nameof(source));
Type guard
bool IsValidSource<TSender, TArgs>(IObservable<EventPattern<TSender, TArgs>>? s) => s is not null;
Try / catch
try { new MyEventPatternSource(source, invoke); } catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* supply fallback observable */ } Prevention
- Construct the observable before the event-pattern source, never lazily as a nullable field.
- Coalesce with Observable.Empty<EventPattern<TSender,TEventArgs>>() for harmless defaults.
When it happens
Trigger: A derived class (or user code subclassing EventPatternSourceBase) passing null for the IObservable<EventPattern<TSender,TEventArgs>> source parameter.
Common situations: Custom event-pattern adapters where the observable is produced by a factory that returned null; refactorings that dropped a null-coalescing operator on the source.
Related errors
- Value cannot be null. (Parameter 'invokeHandler')
- Value cannot be null. (Parameter 'onNextAsync')
- Value cannot be null. (Parameter 'onErrorAsync')
- Value cannot be null. (Parameter 'onCompletedAsync')
- Value cannot be null. (Parameter 'source')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/fb976ec37bc48055.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/EventPatternSourceBase.cs:90
_isAdded = true;
}
}
}
}
private readonly IObservable<EventPattern<TSender, TEventArgs>> _source;
private readonly Dictionary<Delegate, Stack<IDisposable>> _subscriptions = [];
private readonly Action<Action<TSender?, TEventArgs>, /*object,*/ EventPattern<TSender, TEventArgs>> _invokeHandler;
/// <summary>
/// Creates a new event pattern source.
/// </summary>
/// <param name="source">Source sequence to expose as an event.</param>
/// <param name="invokeHandler">Delegate used to invoke the event for each element of the sequence.</param>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="invokeHandler"/> is <c>null</c>.</exception>
protected EventPatternSourceBase(IObservable<EventPattern<TSender, TEventArgs>> source, Action<Action<TSender?, TEventArgs>, /*object,*/ EventPattern<TSender, TEventArgs>> invokeHandler)
{
_source = source ?? throw new ArgumentNullException(nameof(source));
_invokeHandler = invokeHandler ?? throw new ArgumentNullException(nameof(invokeHandler));
}
/// <summary>
/// Adds the specified event handler, causing a subscription to the underlying source.
/// </summary>
/// <param name="handler">Event handler to add. The same delegate should be passed to the <see cref="Remove(Delegate)"/> operation in order to remove the event handler.</param>
/// <param name="invoke">Invocation delegate to raise the event in the derived class.</param>
/// <exception cref="ArgumentNullException"><paramref name="handler"/> or <paramref name="invoke"/> is <c>null</c>.</exception>
protected void Add(Delegate handler, Action<TSender?, TEventArgs> invoke)
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
if (invoke == null)
{View on GitHub (pinned to 94b5d5ab91)