dotnet/reactive · error · ArgumentNullException
Thrown when onNext is null (ArgumentNullException, param…
Error message
Thrown when onNext is null (ArgumentNullException, param name: onNext)
What it means
Documented null-guard failure in EnumerableEx.ForEach: the onNext callback that should be invoked for each enumerated element was null, so the enumeration cannot perform its per-element side effect and throws ArgumentNullException with parameter name 'onNext' before iterating.
Solutions
- Pass a concrete Action<TSource>; use _ => { } if no work is needed per element.
- Ensure the callback is assigned/subscribed before calling ForEach.
- Default the delegate at acquisition: onNext ?? (_ => { }).
Example fix
// before
source.ForEach(handler); // handler may be null
// after
source.ForEach(handler ?? (_ => { })); Defensive patterns
Strategy: validation
Validate before calling
if (onNext is null) throw new ArgumentNullException(nameof(onNext));
source.ForEach(onNext ?? (_ => { })); Type guard
bool HasHandler<TSource>(Action<TSource> a) => a is not null;
Try / catch
try { source.ForEach(action); }
catch (ArgumentNullException ex) when (ex.ParamName == "onNext") { /* attach a handler first */ } Prevention
- Subscribe/assign handlers before iteration.
- Use no-op defaults for optional callbacks.
- Verify event/delegate wiring in setup code.
- Let NRT flag uninitialized delegate members.
When it happens
Trigger: Calling ForEach(source, null) — e.g. a callback resolved from an optional member or event handler that is not attached.
Common situations: Handler not yet subscribed; passing a delegate from configuration that is absent; obj?.Handler producing null.
Related errors
- Thrown when condition is null (ArgumentNullException, param…
- Thrown when finallyAction is null (ArgumentNullException…
- Thrown when resultSelector is null (ArgumentNullException…
- Thrown when selector is null (ArgumentNullException, param…
- Value cannot be null. (Parameter 'handler')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/55b2b5228a9d8bb6.
Report an issue: GitHub.
Appendix: source
Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/ForEach.cs:22
using System.Collections.Generic;
namespace System.Linq
{
public static partial class EnumerableEx
{
/// <summary>
/// Enumerates the sequence and invokes the given action for each value in the sequence.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <param name="source">Source sequence.</param>
/// <param name="onNext">Action to invoke for each element.</param>
public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (onNext == null)
throw new ArgumentNullException(nameof(onNext));
foreach (var item in source)
{
onNext(item);
}
}
/// <summary>
/// Enumerates the sequence and invokes the given action for each value in the sequence.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <param name="source">Source sequence.</param>
/// <param name="onNext">Action to invoke for each element.</param>
public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource, int> onNext)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (onNext == null)View on GitHub (pinned to 94b5d5ab91)