{"record":{"id":"6b5cd1d32580c158","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-aggregate","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'observer')","messagePattern":"Value cannot be null\\. \\(Parameter 'observer'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Aggregate.cs","lineNumber":99,"sourceCode":"                throw new ArgumentNullException(nameof(source));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n            if (func == null)\n                throw new ArgumentNullException(nameof(func));\n\n            return CreateAsyncObservable<TResult>.From(\n                source,\n                (seed, func, resultSelector),\n                static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, state.seed, state.func, state.resultSelector)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> Aggregate<TSource>(IAsyncObserver<TSource> observer, Func<TSource, TSource, TSource> func)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (func == null)\n                throw new ArgumentNullException(nameof(func));\n\n            var hasValue = false;\n            var value = default(TSource);\n\n            return Create<TSource>(\n                async x =>\n                {\n                    if (hasValue)\n                    {\n                        try\n                        {\n                            value = func(value, x);\n                        }\n                        catch (Exception ex)\n                        {\n                            await observer.OnErrorAsync(ex).ConfigureAwait(false);","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Aggregate.cs#L81-L117","documentation":"Thrown by AsyncObserver.Aggregate<TSource>(observer, func) when the downstream IAsyncObserver is null. This is the observer-side pipeline construction API; the observer parameter receives the final aggregated value and must be non-null.","triggerScenarios":"Building a custom subscription pipeline and passing a null observer into AsyncObserver.Aggregate(observer, func), e.g. an uninitialized observer field or a null result of an observer factory.","commonSituations":"Composing AsyncObserver pipelines manually in custom operators; passing through an observer parameter that the caller supplied as null; test scaffolding with incomplete observer setup.","solutions":["Pass a non-null IAsyncObserver<TSource> (e.g. from AsyncObserver.Create or the observer obtained in your operator).","Guard your custom operator: if observer == null throw ArgumentNullException(nameof(observer)) before delegating.","Construct the observer explicitly instead of relying on an uninitialized field."],"exampleFix":"// before\nAsyncObserver.Aggregate(null, (a, x) => a + x);\n// after\nAsyncObserver.Aggregate(AsyncObserver.WriteConsole<int>(), (a, x) => a + x);","handlingStrategy":"validation","validationCode":"if (observer == null) throw new ArgumentNullException(nameof(observer));","typeGuard":"static bool HasObserver<TSource>(IAsyncObserver<TSource> o) => o is not null;","tryCatchPattern":"try { var obs = AsyncObserver.Aggregate(observer, func); } catch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* construct a default observer */ }","preventionTips":["Always derive observer parameters from your operator's OnSubscribeAsync signature rather than fields","Guard custom operators with ArgumentNullException checks at entry","Avoid storing observers in nullable state"],"tags":["csharp","null-argument","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"}