{"record":{"id":"d6c519224b745f08","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-sum-generated","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Sum.Generated.cs","lineNumber":35,"sourceCode":"        }\n\n        public static IAsyncObservable<Int32> Sum<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Int32> selector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (selector == null)\n                throw new ArgumentNullException(nameof(selector));\n\n            return CreateAsyncObservable<Int32>.From(\n                source,\n                selector,\n                static (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.SumInt32(observer, selector)));\n        }\n\n        public static IAsyncObservable<Int32> Sum<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<Int32>> selector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (selector == null)\n                throw new ArgumentNullException(nameof(selector));\n\n            return CreateAsyncObservable<Int32>.From(\n                source,\n                selector,\n                static (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.SumInt32(observer, selector)));\n        }\n\n        public static IAsyncObservable<Int32?> Sum(this IAsyncObservable<Int32?> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.SumNullableInt32(observer)));\n        }\n\n        public static IAsyncObservable<Int32?> Sum<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Int32?> selector)","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Sum.Generated.cs#L17-L53","documentation":"The async-selector overload Sum(source, Func<TSource, ValueTask<int>>) validates the source first and throws ArgumentNullException with message \"Value cannot be null. (Parameter 'source')\" when the observable is null. The operator needs a live sequence to subscribe to.","triggerScenarios":"Invoking the extension on a null receiver (e.g. source-producing factory returned null) or explicitly passing null as the first argument.","commonSituations":"An upstream method that returns IAsyncObservable<T> failing and returning null; optional sequence resolution in configuration; test scaffolding passing null.","solutions":["Ensure the observable is non-null before calling Sum; check the producing method's failure paths.","Substitute AsyncObservable.Empty<int>() (or appropriate type) as a fallback.","Fix the upstream factory/operator that returns null."],"exampleFix":"// before\nvar total = await GetOrders().Sum(o => GetQtyAsync(o)); // GetOrders() returned null\n// after\nvar src = GetOrders() ?? AsyncObservable.Empty<Order>();\nvar total = await src.Sum(o => GetQtyAsync(o));","handlingStrategy":"validation","validationCode":"if (source == null)\n    source = AsyncObservable.Empty<TSource>();","typeGuard":"static bool IsObservable<T>(IAsyncObservable<T>? o) => o is not null;","tryCatchPattern":"try\n{\n    var total = await source.Sum(asyncSelector);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    logger.LogError(ex, \"Null source passed to Sum\");\n    total = 0;\n}","preventionTips":["Make source-producing methods return AsyncObservable.Empty instead of null.","Null-check the result of every factory call before aggregation.","Cover null-source paths in unit tests."],"tags":["argument-null","source","sum","async-rx"],"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"}