{"record":{"id":"2c4fab6cf5087247","repo":"dotnet/reactive","slug":"throw-new-argumentnullexception-nameof-source-observable","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(source));","messagePattern":"throw new ArgumentNullException\\(nameof\\(source\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs","lineNumber":231,"sourceCode":"        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence, and in the lists in the result sequence.</typeparam>\n        /// <param name=\"source\">Source sequence to produce buffers over.</param>\n        /// <param name=\"timeSpan\">Maximum time length of a buffer.</param>\n        /// <param name=\"count\">Maximum element count of a buffer.</param>\n        /// <param name=\"scheduler\">Scheduler to run buffering timers on.</param>\n        /// <returns>An observable sequence of buffers.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"scheduler\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"timeSpan\"/> is less than TimeSpan.Zero. -or- <paramref name=\"count\"/> is less than or equal to zero.</exception>\n        /// <remarks>\n        /// Specifying a TimeSpan.Zero value for <paramref name=\"timeSpan\"/> is not recommended but supported, causing the scheduler to create buffers as fast as it can.\n        /// Because all source sequence elements end up in one of the buffers, some buffers won't have a zero time span. This is a side-effect of the asynchrony introduced\n        /// by the scheduler, where the action to close the current buffer and to create a new buffer may not execute immediately, despite the TimeSpan.Zero due time.\n        /// </remarks>\n        public static IObservable<IList<TSource>> Buffer<TSource>(this IObservable<TSource> source, TimeSpan timeSpan, int count, IScheduler scheduler)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (timeSpan < TimeSpan.Zero)\n            {\n                throw new ArgumentOutOfRangeException(nameof(timeSpan));\n            }\n\n            if (count <= 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(count));\n            }\n\n            if (scheduler == null)\n            {\n                throw new ArgumentNullException(nameof(scheduler));\n            }\n\n            return s_impl.Buffer(source, timeSpan, count, scheduler);","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Time.cs#L213-L249","documentation":"The Buffer(source, timeSpan, count, scheduler) overload throws ArgumentNullException when the source IObservable<TSource> is null. Like all Rx operators, Buffer requires a non-null source sequence and validates arguments eagerly before returning the operator. A null source is a caller bug, not a valid stream.","triggerScenarios":"Calling Observable.Buffer(null, timeSpan, count, scheduler) — commonly from an uninitialized observable field, a factory method returning null, or a conditional lookup that yields null instead of an empty observable.","commonSituations":"Chaining Rx operators off data retrieved from a cache/repository that returns null when missing; nullable reference annotations not enabled so null flows unchecked; refactors where source creation was moved behind a method that can fail silently.","solutions":["Guard the source before the call or substitute Observable.Empty<TSource>()","Fix the upstream producer to return Observable.Empty instead of null","Enable C# nullable reference types so the null path is caught at compile time"],"exampleFix":"// before\nvar buffered = GetStream()?.Buffer(TimeSpan.FromSeconds(1), 5, Scheduler.Default);\n// after\nvar src = GetStream() ?? Observable.Empty<int>();\nvar buffered = src.Buffer(TimeSpan.FromSeconds(1), 5, Scheduler.Default);","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\n// or: source ??= Observable.Empty<TSource>();","typeGuard":"static bool HasSource<T>(IObservable<T>? s) => s is not null;","tryCatchPattern":"try { var b = source.Buffer(timeSpan, count, scheduler); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* fall back to Observable.Empty pipeline */ }","preventionTips":["Never return null observables from factories; use Observable.Empty","Null-check cached/looked-up streams before chaining Rx operators","Enable nullable reference types in the project"],"tags":["rx","argument-validation","null-argument","observable"],"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"}