{"record":{"id":"0c2be3cd90f8a677","repo":"dotnet/reactive","slug":"thrown-when-source-is-null-argumentnullexception-param-name","errorCode":null,"errorMessage":"Thrown when source is null (ArgumentNullException, param name: source)","messagePattern":"Thrown when source is null \\(ArgumentNullException, param name: source\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Do.cs","lineNumber":102,"sourceCode":"            if (onError == null)\n                throw new ArgumentNullException(nameof(onError));\n            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","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Do.cs#L84-L120","documentation":"The Do(source, observer) overload eagerly validates that the source sequence is non-null and throws ArgumentNullException with parameter name 'source' otherwise. The observer form forwards OnNext/OnError/OnCompleted to the given IObserver during enumeration, but the source itself must still be a real sequence.","triggerScenarios":"`null.Do(myObserver)` via extension syntax or Enumerable.Do(null, observer); a nullable source from a lookup or factory piped into the observer-based Do.","commonSituations":"Bridging IEnumerable pipelines into Rx-style observers where the source comes from a repository that can return null; uninitialized test fixtures; conditional pipeline assembly.","solutions":["Coalesce with `?? Enumerable.Empty<T>()` before calling Do","Assert/null-check the source at the pipeline boundary","Make the sequence producer return empty collections instead of null","Only attach the Do(observer) stage when the source is known non-null"],"exampleFix":"// before\nvar result = GetSequence().Do(observer);\n// after\nvar result = (GetSequence() ?? Enumerable.Empty<Event>()).Do(observer);","handlingStrategy":"validation","validationCode":"if (source is null) source = Enumerable.Empty<TSource>();","typeGuard":"static IEnumerable<T> NonNull<T>(IEnumerable<T>? s) => s ?? Enumerable.Empty<T>();","tryCatchPattern":"try { var result = source!.Do(observer); ... }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { Log(\"source sequence was null\"); }","preventionTips":["Coalesce nullable sources before attaching the observer","Have producers return empty sequences rather than null","Assert source non-null in pipeline builders","Enable nullable reference types"],"tags":["argumentnull","linq","dotnet","observer","null-sequence"],"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"}