{"record":{"id":"99d3ce4baab31e17","repo":"dotnet/reactive","slug":"throw-new-argumentnullexception-nameof-source-sample","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(source));","messagePattern":"throw new ArgumentNullException\\(nameof\\(source\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Sample.cs","lineNumber":17,"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.Concurrency;\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<TSource> Sample<TSource, TSample>(this IAsyncObservable<TSource> source, IAsyncObservable<TSample> sampler)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (sampler == null)\n                throw new ArgumentNullException(nameof(sampler));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                sampler,\n                static async (source, sampler, observer) =>\n                {\n                    var (sourceSink, samplerSink) = AsyncObserver.Sample<TSource, TSample>(observer);\n\n                    var sourceSubscription = await source.SubscribeSafeAsync(sourceSink).ConfigureAwait(false);\n                    var samplerSubscription = await sampler.SubscribeSafeAsync(samplerSink).ConfigureAwait(false);\n\n                    return StableCompositeAsyncDisposable.Create(sourceSubscription, samplerSubscription);\n                });\n        }\n\n        public static IAsyncObservable<TSource> Sample<TSource>(this IAsyncObservable<TSource> source, TimeSpan interval)","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Sample.cs#L1-L35","documentation":"The Sample<TSource,TSample>(source, sampler) extension throws ArgumentNullException when the source async observable is null. System.Reactive.Async validates all public arguments eagerly at call time rather than failing later inside the subscription pipeline, so you get a synchronous, precise stack trace instead of a deferred NRE when the operator body runs.","triggerScenarios":"Calling source.Sample(sampler) where the IAsyncObservable<TSource> variable is null — typically because a factory method, dictionary lookup, or conditional composition returned null instead of an observable.","commonSituations":"A helper method returns null when an event stream is not yet initialized; a memoization cache miss yields null; refactoring moved the observable creation after the Sample call; DI container registered a null binding.","solutions":["Ensure the source observable is created before calling Sample (e.g. AsyncObservable.Interval, FromEvent, etc.) and that no factory returns null","Add a null check at the composition site and throw a descriptive exception or fall back to an empty observable","Use the null-coalescing pattern: (maybeSource ?? AsyncObservable.Empty<TSource>()).Sample(sampler)","Check upstream library/API that should supply the observable for documented null returns"],"exampleFix":"// before\nIAsyncObservable<Price> prices = GetPriceStream(); // may return null\nvar sampled = prices.Sample(ticks);\n// after\nvar src = GetPriceStream() ?? AsyncObservable.Empty<Price>();\nvar sampled = src.Sample(ticks);","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\nvar sampled = source.Sample(sampler);","typeGuard":"static bool IsNotNull<T>(T? arg) where T : class => arg is not null;","tryCatchPattern":"try\n{\n    var sampled = source.Sample(sampler);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    // log and fall back to an empty stream\n    sampled = AsyncObservable.Empty<TSource>();\n}","preventionTips":["Never let factory methods return null observables; return AsyncObservable.Empty<T> instead","Enable C# nullable reference types so null flows are flagged at compile time","Validate arguments at your own composition boundaries before calling operators","Prefer (stream ?? AsyncObservable.Empty<T>()) when a stream is genuinely optional"],"tags":["csharp","null-argument","async-reactive","argument-validation"],"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"}