{"record":{"id":"14c41dc7552db006","repo":"dotnet/reactive","slug":"thrown-when-observer-is-null-argumentnullexception-param","errorCode":null,"errorMessage":"Thrown when observer is null (ArgumentNullException, param name: observer)","messagePattern":"Thrown when observer is null \\(ArgumentNullException, param name: observer\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Do.cs","lineNumber":104,"sourceCode":"            if (onCompleted == null)\n                throw new ArgumentNullException(nameof(onCompleted));\n\n            return DoCore(source, onNext, onError, onCompleted);\n        }\n\n        /// <summary>\n        /// Lazily invokes observer methods for each value in the sequence, and upon successful or exceptional termination.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"observer\">Observer to invoke notification calls on.</param>\n        /// <returns>Sequence exhibiting the side-effects of observer method invocation upon enumeration.</returns>\n        public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, IObserver<TSource> observer)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            return DoCore(source, observer.OnNext, observer.OnError, observer.OnCompleted);\n        }\n\n        private static IEnumerable<TSource> DoCore<TSource>(IEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)\n        {\n            using var e = source.GetEnumerator();\n\n            while (true)\n            {\n                TSource current;\n                try\n                {\n                    if (!e.MoveNext())\n                        break;\n\n                    current = e.Current;\n                }","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Do.cs#L86-L122","documentation":"The Do(source, observer) overload requires a non-null IObserver<TSource> and throws ArgumentNullException with parameter name 'observer'. The observer's OnNext, OnError, and OnCompleted methods are captured and invoked during enumeration, so null is rejected eagerly at the call site.","triggerScenarios":"`source.Do(null)`; an observer resolved from DI or a registry that returned null; a field/property of observer type never assigned before pipeline construction.","commonSituations":"Observer registrations missing in the DI container; test doubles (mock observers) not initialized; conditional observer attachment where the observer is null in some environments.","solutions":["Supply a real IObserver implementation or a test double before calling Do","Default to a no-op observer: `observer ??= NullObserver<T>.Instance;` (or an anonymous implementation)","Fix the DI/registry wiring that yields a null observer","Guard the stage: only add Do(observer) when observer != null"],"exampleFix":"// before\nvar result = source.Do(null);\n// after\nvar result = source.Do(new AnonymousObserver<T>(x => Log(x)));\n// or\nobserver ??= new NopObserver<T>();\nvar result = source.Do(observer);","handlingStrategy":"validation","validationCode":"if (observer is null) throw new InvalidOperationException(\"Observer must be registered before composing Do.\");\n// or default:\n// if (observer is null) observer = new NopObserver<T>();","typeGuard":"static bool HasObserver<T>(IObserver<T>? o) => o is not null;","tryCatchPattern":"try { var result = source.Do(observer!); ... }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { Log(\"observer missing; attaching no-op observer\"); result = source; }","preventionTips":["Register observers in DI so resolution never returns null","Provide a shared NopObserver<T>/AnonymousObserver<T> default","Check HasObserver before composing the Do stage","Use the callback-based overloads if you only have lambdas, not an IObserver"],"tags":["argumentnull","linq","dotnet","observer"],"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"}