{"record":{"id":"3d310cff0d2fa9b5","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-selector-sum-generated","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'selector')","messagePattern":"Value cannot be null\\. \\(Parameter 'selector'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Sum.Generated.cs","lineNumber":24,"sourceCode":"\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\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.SumInt32(observer)));\n        }\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)));","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Sum.Generated.cs#L6-L42","documentation":"The generated Sum(source, selector) operator validates both arguments and throws ArgumentNullException with the full framework message \"Value cannot be null. (Parameter 'selector')\" when the projection delegate is null. The selector is required to convert each element to Int32 before aggregation.","triggerScenarios":"Calling source.Sum(null) or passing a selector variable that is null — e.g. a Func stored in config/DI that was never registered, or a method group that failed to bind (wrong signature).","commonSituations":"A delegate field initialized lazily and read too early; a method-group reference removed in a refactor; JSON/deserialized pipelines where delegates can't be expressed and default to null.","solutions":["Pass a concrete selector, e.g. source.Sum(x => x.Amount).","Fix the null delegate source (DI registration, field initialization, method group signature).","If no projection is needed, call the selector-less Sum() overload."],"exampleFix":"// before\nFunc<Order, int> sel = null;\nvar total = orders.Sum(sel);\n// after\nvar total = orders.Sum(o => o.Amount);","handlingStrategy":"validation","validationCode":"if (selector == null)\n    throw new ArgumentNullException(nameof(selector));\nvar total = source.Sum(selector);","typeGuard":"bool HasSelector<TSource>(Func<TSource, int>? f) => f is not null;","tryCatchPattern":"try\n{\n    var total = await source.Sum(selector);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"selector\")\n{\n    logger.LogError(ex, \"Null selector passed to Sum\");\n    total = 0;\n}","preventionTips":["Always pass selector lambdas inline rather than through nullable delegate variables.","If delegates come from configuration, replace with named/registered selector functions.","Use nullable reference types to catch unassigned Func fields."],"tags":["argument-null","selector","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"}