{"record":{"id":"e8af75544816055a","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-onnext-observable-blocking","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'onNext')","messagePattern":"Value cannot be null\\. \\(Parameter 'onNext'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Blocking.cs","lineNumber":223,"sourceCode":"        /// <summary>\n        /// Invokes an action for each element in the observable sequence, and blocks until the sequence is terminated.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"onNext\">Action to invoke for each element in the observable sequence.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"onNext\"/> is null.</exception>\n        /// <remarks>Because of its blocking nature, this operator is mainly used for testing.</remarks>\n        [Obsolete(Constants_Linq.UseAsync)]\n        public static void ForEach<TSource>(this IObservable<TSource> source, Action<TSource> onNext)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (onNext == null)\n            {\n                throw new ArgumentNullException(nameof(onNext));\n            }\n\n            s_impl.ForEach(source, onNext);\n        }\n\n        /// <summary>\n        /// Invokes an action for each element in the observable sequence, incorporating the element's index, and blocks until the sequence is terminated.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"onNext\">Action to invoke for each element in the observable sequence.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"onNext\"/> is null.</exception>\n        /// <remarks>Because of its blocking nature, this operator is mainly used for testing.</remarks>\n        [Obsolete(Constants_Linq.UseAsync)]\n        public static void ForEach<TSource>(this IObservable<TSource> source, Action<TSource, int> onNext)\n        {\n            if (source == null)\n            {","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Blocking.cs#L205-L241","documentation":"The blocking ForEach invokes onNext for every element while blocking the caller until termination; a null action is rejected up front because every notification would fail. Fix: pass a non-null Action<TSource>, or use Subscribe if you need non-blocking behavior.","triggerScenarios":"Calling Observable.ForEach<T>(source, null), e.g. a callback field that was never assigned.","commonSituations":"Optional callback parameters passed straight through; event-handler delegates resolved from DI or configuration that ended up null.","solutions":["Pass a non-null action or use Subscribe with default (empty) behavior instead","Default the action to _ => { } when optional","Guard onNext != null before calling ForEach"],"exampleFix":"// before\nsource.ForEach(maybeAction);\n// after\nsource.ForEach(maybeAction ?? (_ => { }));","handlingStrategy":"validation","validationCode":"if (onNext == null) throw new InvalidOperationException(\"onNext callback is required\");","typeGuard":"bool HasAction<T>(Action<T>? a) => a is not null;","tryCatchPattern":"try { source.ForEach(onNext); } catch (ArgumentNullException ex) when (ex.ParamName == \"onNext\") { /* supply a default action */ }","preventionTips":["Default optional callbacks to a no-op (_ => { }) instead of null","Check that event-handler fields are assigned before use","Pass method groups directly so the compiler verifies non-null"],"tags":["csharp","rx","null-argument","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-15T23:17:13.987Z"}