{"record":{"id":"5dca0d668c8a3ddb","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-second-withlatestfrom","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'second')","messagePattern":"Value cannot be null\\. \\(Parameter 'second'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/WithLatestFrom.cs","lineNumber":18,"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.Reactive.Disposables;\nusing System.Threading;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<(TFirst first, TSecond second)> WithLatestFrom<TFirst, TSecond>(this IAsyncObservable<TFirst> first, IAsyncObservable<TSecond> second)\n        {\n            if (first == null)\n                throw new ArgumentNullException(nameof(first));\n            if (second == null)\n                throw new ArgumentNullException(nameof(second));\n\n            return CreateAsyncObservable<(TFirst first, TSecond second)>.From(\n                first,\n                second,\n                static async (first, second, observer) =>\n                {\n                    var (firstObserver, secondObserver) = AsyncObserver.WithLatestFrom(observer);\n\n                    // REVIEW: Consider concurrent subscriptions.\n\n                    var firstSubscription = await first.SubscribeSafeAsync(firstObserver).ConfigureAwait(false);\n                    var secondSubscription = await second.SubscribeSafeAsync(secondObserver).ConfigureAwait(false);\n\n                    return StableCompositeAsyncDisposable.Create(firstSubscription, secondSubscription);\n                });\n        }\n\n        public static IAsyncObservable<TResult> WithLatestFrom<TFirst, TSecond, TResult>(this IAsyncObservable<TFirst> first, IAsyncObservable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/WithLatestFrom.cs#L1-L36","documentation":"WithLatestFrom requires a non-null `second` observable whose latest emitted value is paired with each value of the source. The extension method throws ArgumentNullException with parameter name 'second' when that argument is null, before creating the combined observable.","triggerScenarios":"Calling first.WithLatestFrom(second) (the two-parameter overload) with a null second argument, e.g. when the secondary stream is loaded lazily/conditionally and is null at pipeline build time.","commonSituations":"Telemetry/UI scenarios where the 'other' stream comes from an optional service that failed to initialize; pipelines configured from config files with a missing secondary source key; refactors where the second observable was renamed and an old null default remained.","solutions":["Provide a valid IAsyncObservable<TSecond> for `second` before building the pipeline.","If the secondary stream may be absent, use an empty or never-completing observable rather than null.","Initialize the secondary source earlier in startup so it is available when the pipeline is built.","Add a null check with a clear error message where the second source is obtained."],"exampleFix":"// before\nvar result = first.WithLatestFrom(second); // second is null\n// after\nsecond ??= AsyncObservable.Never<TSecond>();\nvar result = first.WithLatestFrom(second);","handlingStrategy":"validation","validationCode":"if (second is null) throw new ArgumentNullException(nameof(second));\nvar result = first.WithLatestFrom(second);","typeGuard":"bool IsSecondValid<T>(IAsyncObservable<T> o) => o is not null;","tryCatchPattern":"try\n{\n    var result = first.WithLatestFrom(second);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"second\")\n{\n    // substitute a never-completing stream so nothing is emitted from `second`\n    var result = first.WithLatestFrom(AsyncObservable.Never<TSecond>());\n}","preventionTips":["Build optional secondary streams as Never/Empty observables instead of null.","Initialize all source observables before composing the pipeline.","Validate config/DI-resolved observables for non-null right after resolution.","Enable nullable reference types to catch null arguments at compile time."],"tags":["argument-null","async-rx","withlatestfrom-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"}