{"record":{"id":"55b2b5228a9d8bb6","repo":"dotnet/reactive","slug":"thrown-when-onnext-is-null-argumentnullexception-param-name","errorCode":null,"errorMessage":"Thrown when onNext is null (ArgumentNullException, param name: onNext)","messagePattern":"Thrown when onNext is null \\(ArgumentNullException, param name: onNext\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/ForEach.cs","lineNumber":22,"sourceCode":"\nusing System.Collections.Generic;\n\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Enumerates the sequence and invokes the given action for each value in the sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"onNext\">Action to invoke for each element.</param>\n        public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (onNext == null)\n                throw new ArgumentNullException(nameof(onNext));\n\n            foreach (var item in source)\n            {\n                onNext(item);\n            }\n        }\n\n        /// <summary>\n        /// Enumerates the sequence and invokes the given action for each value in the sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"onNext\">Action to invoke for each element.</param>\n        public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource, int> onNext)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (onNext == null)","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/ForEach.cs#L4-L40","documentation":"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.","triggerScenarios":"Calling ForEach(source, null) — e.g. a callback resolved from an optional member or event handler that is not attached.","commonSituations":"Handler not yet subscribed; passing a delegate from configuration that is absent; obj?.Handler producing null.","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 ?? (_ => { })."],"exampleFix":"// before\nsource.ForEach(handler); // handler may be null\n// after\nsource.ForEach(handler ?? (_ => { }));","handlingStrategy":"validation","validationCode":"if (onNext is null) throw new ArgumentNullException(nameof(onNext));\nsource.ForEach(onNext ?? (_ => { }));","typeGuard":"bool HasHandler<TSource>(Action<TSource> a) => a is not null;","tryCatchPattern":"try { source.ForEach(action); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"onNext\") { /* attach a handler first */ }","preventionTips":["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."],"tags":["null-argument","argument-validation","delegate"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}