{"record":{"id":"51af0ab2a408b421","repo":"dotnet/reactive","slug":"value-cannot-be-null","errorCode":null,"errorMessage":"Value cannot be null.","messagePattern":"Value cannot be null\\.","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Average.Generated.cs","lineNumber":124,"sourceCode":"\n            return CreateAsyncObservable<Double>.From(\n                source,\n                selector,\n                static (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.AverageInt64(observer, selector)));\n        }\n\n        public static IAsyncObservable<Double?> Average(this IAsyncObservable<Int64?> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create<Int64?, Double?>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.AverageNullableInt64(observer)));\n        }\n\n        public static IAsyncObservable<Double?> Average<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Int64?> 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<Double?>.From(\n                source,\n                selector,\n                static (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.AverageNullableInt64(observer, selector)));\n        }\n\n        public static IAsyncObservable<Double?> Average<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<Int64?>> 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<Double?>.From(\n                source,","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Average.Generated.cs#L106-L142","documentation":"This generated overload of Average (selector returning Int64?) validates both arguments and throws ArgumentNullException when source is null. Generated average operators in AsyncRx.NET follow the same eager-validation pattern: a null source cannot be subscribed to, so the call fails immediately at line 124 of Average.Generated.cs.","triggerScenarios":"Calling Average<TSource>(source, selector) where source is null and selector is the Func<TSource, Int64?> form — e.g. items.AsAsyncObservable() was actually null, or a prior operator returned null.","commonSituations":"A repository or service method returning null instead of an empty observable; LINQ-style chains built from nullable results of earlier queries; code generated or refactored where an upstream null-check was dropped.","solutions":["Ensure the source is non-null before aggregating: use AsyncObservable.Empty<TSource>() as a fallback.","Fix the producer that returned null instead of an empty observable.","Add a null assertion (ArgumentNullException.ThrowIfNull(source) or if-check) where the source is obtained.","If migrating from System.Reactive, note null sources are equally invalid there — locate where the null originated."],"exampleFix":"// before\nvar avg = (await GetSourceAsync()).Average(x => (long?)x.Value); // GetSourceAsync returned null\n\n// after\nvar src = await GetSourceAsync() ?? AsyncObservable.Empty<Item>();\nvar avg = src.Average(x => (long?)x.Value);","handlingStrategy":"validation","validationCode":"if (source == null)\n    throw new InvalidOperationException(\"source stream not initialized before Average\");","typeGuard":"static bool HasSource<T>(IAsyncObservable<T>? s) => s is not null;","tryCatchPattern":"try\n{\n    var avg = source.Average(x => (long?)x.Value);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    // handle missing stream: log, substitute empty, or rethrow with context\n}","preventionTips":["Treat null observables as bugs; normalize to Empty<T>() at the boundary","Enable nullable reference types (enable) so the compiler flags null flows","Unit-test producers for the empty case"],"tags":["dotnet","asyncrx","argument-null","null-source","average-operator"],"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"}