{"record":{"id":"4b71eb19024909d1","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-buffer","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Buffer.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.Collections.Generic;\nusing System.Reactive.Concurrency;\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 IAsyncObservable<IList<TSource>> Buffer<TSource>(this IAsyncObservable<TSource> source, int count)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (count <= 0)\n                throw new ArgumentNullException(nameof(count));\n\n            return CreateAsyncObservable<IList<TSource>>.From(\n                source,\n                count,\n                static (source, count, observer) => source.SubscribeSafeAsync(AsyncObserver.Buffer(observer, count)));\n        }\n\n        public static IAsyncObservable<IList<TSource>> Buffer<TSource>(this IAsyncObservable<TSource> source, int count, int skip)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (count <= 0)\n                throw new ArgumentNullException(nameof(count));\n            if (skip <= 0)\n                throw new ArgumentNullException(nameof(skip));\n","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Buffer.cs#L1-L36","documentation":"The Buffer(count) extension validates its source IAsyncObservable<TSource> and throws ArgumentNullException('observer' wording notwithstanding, the parameter here is 'source') when null. Buffer groups elements into IList<TSource> batches of the given size, which is impossible without a source. AsyncRx.NET validates all operator inputs at composition time so failures happen where the pipeline is built, not mid-stream.","triggerScenarios":"Calling source.Buffer(count) where source is null — e.g. a method returning IAsyncObservable<T> that returned null on an error path, an uninitialized field, or a null result from a lookup/conditional factory before chaining .Buffer(n).","commonSituations":"Conditional observable creation (returning null instead of AsyncObservable.Empty<T>()); refactoring that removed the assignment feeding the variable; optional configuration where the observable is only set in some environments.","solutions":["Ensure the expression producing the source is non-null before chaining .Buffer(count); null-check or coalesce to AsyncObservable.Empty<TSource>().","Fix the factory/repository method that returned a null IAsyncObservable so it returns a valid (possibly empty) observable.","Catch ArgumentNullException at the composition site to fail fast with a clearer domain message."],"exampleFix":"// before\nvar batches = GetObservable()?.Buffer(10); // null source when factory fails\n// after\nvar src = GetObservable() ?? AsyncObservable.Empty<MyItem>();\nvar batches = src.Buffer(10);","handlingStrategy":"validation","validationCode":"if (source is null) source = AsyncObservable.Empty<TSource>();\nif (count <= 0) throw new ArgumentException(\"count must be positive\", nameof(count));","typeGuard":"bool IsSubscribable<T>(IAsyncObservable<T>? s) => s is not null;","tryCatchPattern":"try { var batches = src.Buffer(count); }\ncatch (ArgumentNullException ex) { log.LogError(ex, \"Buffer composition failed: {Param}\", ex.ParamName); throw; }","preventionTips":["Coalesce null observables with AsyncObservable.Empty<T>() before chaining operators","Make factories return non-null (possibly empty) observables rather than null","Enable nullable reference types to catch null sources at compile time"],"tags":["argumentnull","asyncrx","buffer","null-source"],"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"}