{"record":{"id":"6e1ae06616258acd","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-observable-aggregates","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":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Aggregates.cs","lineNumber":30,"sourceCode":"        #region + Aggregate +\n\n        /// <summary>\n        /// Applies an accumulator function over an observable sequence, returning the result of the aggregation as a single element in the result sequence. The specified seed value is used as the initial accumulator value.\n        /// For aggregation behavior with incremental intermediate results, see <see cref=\"Observable.Scan{TSource, Accumulate}\"/>.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <typeparam name=\"TAccumulate\">The type of the result of the aggregation.</typeparam>\n        /// <param name=\"source\">An observable sequence to aggregate over.</param>\n        /// <param name=\"seed\">The initial accumulator value.</param>\n        /// <param name=\"accumulator\">An accumulator function to be invoked on each element.</param>\n        /// <returns>An observable sequence containing a single element with the final accumulator value.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"accumulator\"/> is null.</exception>\n        /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>\n        public static IObservable<TAccumulate> Aggregate<TSource, TAccumulate>(this IObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (accumulator == null)\n            {\n                throw new ArgumentNullException(nameof(accumulator));\n            }\n\n            return s_impl.Aggregate(source, seed, accumulator);\n        }\n\n        /// <summary>\n        /// Applies an accumulator function over an observable sequence, returning the result of the aggregation as a single element in the result sequence. The specified seed value is used as the initial accumulator value,\n        /// and the specified result selector function is used to select the result value.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <typeparam name=\"TAccumulate\">The type of the accumulator value.</typeparam>\n        /// <typeparam name=\"TResult\">The type of the resulting value.</typeparam>\n        /// <param name=\"source\">An observable sequence to aggregate over.</param>","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Aggregates.cs#L12-L48","documentation":"The seeded Aggregate extension validates its arguments eagerly and throws ArgumentNullException when the source observable is null. This is a synchronous, immediate fail-fast check performed at call time so the caller gets a precise stack trace instead of a cryptic failure inside the pipeline.","triggerScenarios":"Invoking Observable.Aggregate(source, seed, accumulator) where the source IObservable<TSource> reference is null (e.g. a factory method returned null, an uninitialized field, or a dictionary lookup missed).","commonSituations":"Chaining operators off a method that can return null instead of an empty observable; conditional composition where the base stream was never assigned; DI containers yielding null for an unregistered stream dependency.","solutions":["Ensure the source is non-null before calling Aggregate: use the ?? operator to substitute Observable.Empty<TSource>().","Fix the producer/method that returns null instead of an empty observable; returning null from an Rx factory is a bug.","Enable nullable reference types (C# 8 #nullable enable) so the compiler flags possibly-null sources.","Check arguments with Arg.NotNull / ArgumentNullException.ThrowIfNull at your own API boundary to catch it early."],"exampleFix":"// before\nvar result = await GetStream().Aggregate(0, (acc, x) => acc + x); // NRE risk if GetStream() returns null\n\n// after\nvar src = GetStream() ?? Observable.Empty<int>();\nvar result = await src.Aggregate(0, (acc, x) => acc + x);","handlingStrategy":"type-guard","validationCode":"if (source is null) source = Observable.Empty<TSource>();\nArgumentNullException.ThrowIfNull(source); // optional explicit guard","typeGuard":"static IObservable<T> NotNull<T>(IObservable<T>? s) => s ?? Observable.Empty<T>();","tryCatchPattern":"try { var result = source.Aggregate(seed, acc); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* recover: substitute empty stream or report caller bug */ }","preventionTips":["Never let Rx factory methods return null; return Observable.Empty instead.","Enable C# nullable reference types so null sources are caught at compile time.","Coalesce nullable stream references with ?? Observable.Empty<T>() at call sites.","Validate inputs at your own API boundary before handing streams to Rx operators."],"tags":["rx","argument-null","aggregate-operator","fail-fast"],"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"}