{"record":{"id":"f1b007163755e08c","repo":"dotnet/reactive","slug":"argument-cannot-be-null-parameter-name-onnext","errorCode":null,"errorMessage":"Argument cannot be null (Parameter name: onNext)","messagePattern":"Argument cannot be null \\(Parameter name: onNext\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ForEachAsync.cs","lineNumber":18,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Reactive.Disposables;\nusing System.Threading;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static Task ForEachAsync<TSource>(this IAsyncObservable<TSource> source, Action<TSource> onNext, CancellationToken token = default)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (onNext == null)\n                throw new ArgumentNullException(nameof(onNext));\n\n            return ForEachAsyncCore(source, (x, i) => { onNext(x); return Task.CompletedTask; }, token);\n        }\n\n        public static Task ForEachAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Task> onNext, CancellationToken token = default)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (onNext == null)\n                throw new ArgumentNullException(nameof(onNext));\n\n            return ForEachAsyncCore(source, (x, i) => onNext(x), token);\n        }\n\n        public static Task ForEachAsync<TSource>(this IAsyncObservable<TSource> source, Action<TSource, int> onNext, CancellationToken token = default)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ForEachAsync.cs#L1-L36","documentation":"ForEachAsync(IAsyncObservable<TSource>, Action<TSource>, CancellationToken) throws ArgumentNullException because the onNext action is null. The synchronous-callback overload requires a non-null Action<TSource> that is invoked for every element; the argument is validated up front with the parameter name 'onNext'. A null action cannot be interpreted as 'do nothing' — use an empty lambda instead.","triggerScenarios":"Calling source.ForEachAsync((Action<TSource>)null) — passing an unassigned delegate field, a callback parameter that the caller omitted, or a conditional handler assignment that stayed null.","commonSituations":"Event-handler wiring where the handler is attached later; plugin architectures where the user-supplied handler is optional; unit tests passing null to check behavior.","solutions":["Pass a real handler, e.g. x => Console.WriteLine(x) or an empty x => { } when nothing needs doing.","If the handler is optional, coalesce: onNext ?? (x => { }).","Ensure the delegate field/property is assigned before the pipeline is started.","Validate handler presence at configuration time and reject the pipeline early with a clear message."],"exampleFix":"// before\nAction<int> handler = _handlers.Get(\"onNext\"); // may be null\nawait source.ForEachAsync(handler);\n\n// after\nawait source.ForEachAsync(_handlers.Get(\"onNext\") ?? (x => { }));","handlingStrategy":"validation","validationCode":"if (onNext == null)\n    onNext = _ => { }; // or throw with context\nreturn source.ForEachAsync(onNext, token);","typeGuard":"static bool IsNonNullHandler<T>(Action<T> h) => h is not null;","tryCatchPattern":"try { await source.ForEachAsync(onNext, token); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"onNext\") { /* attach default handler or report missing handler config */ }","preventionTips":["Use x => { } instead of null for 'do nothing' handlers.","Coalesce optional handlers at the call site (onNext ?? (_ => { })).","Assert handlers are wired before starting pipelines in integration tests."],"tags":["asyncrx","argument-null","onnext","foreachasync"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}