dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'eventName')
Error message
Value cannot be null. (Parameter 'eventName')
What it means
Observable.FromEventPattern(object target, string eventName) throws ArgumentNullException when eventName is null. The event name string is the key used for reflection lookup on the target's type, so Rx validates it as a required argument before attempting any reflection.
Solutions
- Pass the event name as a literal or use nameof(target.Click), which also gives compile-time safety.
- Initialize the configuration/constant supplying the event name.
- Validate the name string before the call if it is dynamic.
- Prefer the strongly-typed FromEventPattern overloads (e.g. with Action<EventHandler>) that avoid reflection by name entirely.
Example fix
// before string name = config["EventName"]; // null var xs = Observable.FromEventPattern(target, name); // after var xs = Observable.FromEventPattern(target, nameof(target.Click));
Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(eventName)) throw new ArgumentException("Event name required", nameof(eventName)); Type guard
bool HasEventName(string eventName) => !string.IsNullOrEmpty(eventName);
Try / catch
try { var xs = Observable.FromEventPattern(target, eventName); }
catch (ArgumentNullException ex) when (ex.ParamName == "eventName") { /* fall back to literal name */ } Prevention
- Always use nameof(...) for event names to get compile-time safety.
- Never source event names from nullable config without a fallback.
- Centralize event-name constants in one validated location.
- Prefer delegate-based FromEventPattern overloads to skip reflection by name.
When it happens
Trigger: Calling Observable.FromEventPattern(target, null) — target is a valid object but the event name string is null (eventName guard at line 554).
Common situations: Event name stored in a config/constant that was never set; using nameof() on a null-typed member or building the name dynamically and getting null; a rename removed the constant's value.
Related errors
- Value cannot be null. (Parameter 'target')
- action
- action (Parameter 'action')
- action (Parameter 'action')
- action (Value cannot be null)
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/1f32048c0d535a0d.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Events.cs:554
/// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so
/// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions
/// more concise and easier to understand.
/// </para>
/// </remarks>
/// <seealso cref="ToEventPattern"/>
#if HAS_TRIMMABILITY_ATTRIBUTES
[RequiresUnreferencedCode(Constants_Core.EventReflectionTrimIncompatibilityMessage)]
#endif
public static IObservable<EventPattern<object>> FromEventPattern(object target, string eventName)
{
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
if (eventName == null)
{
throw new ArgumentNullException(nameof(eventName));
}
return s_impl.FromEventPattern(target, eventName);
}
/// <summary>
/// Converts an instance .NET event, conforming to the standard .NET event pattern with an <see cref="EventArgs"/> parameter, to an observable sequence.
/// Each event invocation is surfaced through an OnNext message in the resulting sequence.
/// Reflection is used to discover the event based on the target object type and the specified event name.
/// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.
/// </summary>
/// <param name="target">Object instance that exposes the event to convert.</param>
/// <param name="eventName">Name of the event to convert.</param>
/// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param>
/// <returns>The observable sequence that contains data representations of invocations of the underlying .NET event.</returns>
/// <exception cref="ArgumentNullException"><paramref name="target"/> or <paramref name="eventName"/> or <paramref name="scheduler"/> is null.</exception>
/// <exception cref="InvalidOperationException">The event could not be found. -or- The event does not conform to the standard .NET event pattern.</exception>
/// <remarks>View on GitHub (pinned to 94b5d5ab91)