{"record":{"id":"9ed9849b4e74b185","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-max","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/Max.cs","lineNumber":15,"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.Collections.Generic;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> Max<TSource>(IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Max(observer)));\n        }\n\n        public static IAsyncObservable<TSource> Max<TSource>(IAsyncObservable<TSource> source, IComparer<TSource> comparer)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (comparer == null)\n                throw new ArgumentNullException(nameof(comparer));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                comparer,\n                static (source, comparer, observer) => source.SubscribeSafeAsync(AsyncObserver.Max(observer, comparer)));\n        }\n    }\n","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Max.cs#L1-L33","documentation":"The parameterless-projection AsyncObservable.Max<TSource>(IAsyncObservable<TSource>) throws ArgumentNullException with parameter name 'source' when the input async observable is null. The library validates all operator inputs eagerly so the failure surfaces at composition time, not later during subscription. A null source has no meaningful Max semantics, so the throw is immediate.","triggerScenarios":"Calling AsyncObservable.Max(null) — typically a source variable that is null because an earlier factory/conditional expression produced null, or a method chain where the upstream Build/Return call was skipped.","commonSituations":"A pipeline assembled conditionally where the source assignment is missed on one branch; deserialized or DI-injected observables that failed to bind; storing IAsyncObservable in a field initialized lazily and read too early.","solutions":["Ensure the source is created with a non-null factory such as AsyncObservable.Return(...) or AsyncObservable.Empty(...) before calling Max.","Guard at the call site: if (source == null) source = AsyncObservable.Empty<int>();","Use the null-coalescing operator: AsyncObservable.Max(source ?? AsyncObservable.Empty<int>())"],"exampleFix":"// before\nIAsyncObservable<int> source = maybeSource; // can be null\nvar max = AsyncObservable.Max(source);\n\n// after\nvar max = AsyncObservable.Max(source ?? AsyncObservable.Empty<int>());","handlingStrategy":"validation","validationCode":"if (source is null) source = AsyncObservable.Empty<TSource>();","typeGuard":"static bool HasSource<TSource>(IAsyncObservable<TSource>? s) => s is not null;","tryCatchPattern":"try { var max = AsyncObservable.Max(source); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* substitute Empty() or fix upstream factory */ }","preventionTips":["Build sources only via library factories (Return/Range/Empty), never leave pipeline variables null","Initialize IAsyncObservable fields at declaration or in constructors","Check DI registrations for unbound IAsyncObservable dependencies"],"tags":["dotnet","null-argument","asyncrx","max-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"}