{"record":{"id":"815fc63a8e9c71d5","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-aggregate","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/Aggregate.cs","lineNumber":14,"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.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> Aggregate<TSource>(this IAsyncObservable<TSource> source, Func<TSource, TSource, TSource> func)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (func == null)\n                throw new ArgumentNullException(nameof(func));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                func,\n                static (source, func, observer) => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, func)));\n        }\n\n        public static IAsyncObservable<TSource> Aggregate<TSource>(this IAsyncObservable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> func)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (func == null)\n                throw new ArgumentNullException(nameof(func));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Aggregate.cs#L1-L32","documentation":"AsyncObservable.Aggregate validates its inputs and throws ArgumentNullException when source is null ('Value cannot be null. (Parameter source)'). The aggregate operator needs a real observable to reduce over; a null source cannot produce a seed or result. The guard runs synchronously before the operator pipeline is constructed.","triggerScenarios":"Calling AsyncObservable.Aggregate(null, func) — e.g. the observable came from an operator/factory that returned null, or an unassigned IAsyncObservable<T> field was passed.","commonSituations":"Conditional query construction where a branch skipped creating the source; DI or configuration returning null for an observable dependency; passing the result of an expression like GetSource() that can return null.","solutions":["Ensure the source is created before aggregation, e.g. via AsyncObservable.Create or another operator.","Check the factory/method that produced the source for null-returning paths.","Add a null check or assert at the point of source construction to surface the origin earlier."],"exampleFix":"// before\nvar sum = await AsyncObservable.Aggregate(source, (acc, x) => acc + x); // source was null\n// after\nif (source == null) throw new InvalidOperationException(\"source not initialized\");\nvar sum = await AsyncObservable.Aggregate(source, (acc, x) => acc + x);","handlingStrategy":"validation","validationCode":"if (source is null) throw new InvalidOperationException(\"source must be non-null for Aggregate\");\nvar result = await AsyncObservable.Aggregate(source, func);","typeGuard":"bool IsValidSource<T>(IAsyncObservable<T> s) => s is not null;","tryCatchPattern":"try\n{\n    var result = await AsyncObservable.Aggregate(source, func);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    // source construction failed upstream; log and provide fallback/default observable\n}","preventionTips":["Verify every operator in the query chain returns non-null before passing to Aggregate.","Initialize IAsyncObservable fields eagerly or with required/readonly modifiers.","Enable nullable reference types to catch null source flow at compile time.","Guard factory methods that produce observables so they throw instead of returning null."],"tags":["csharp","null-check","operators","argument-validation"],"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"}