{"record":{"id":"1a16b0f61c47fed1","repo":"dotnet/reactive","slug":"throw-new-argumentnullexception-nameof-observer-takewhile","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(observer));","messagePattern":"throw new ArgumentNullException\\(nameof\\(observer\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeWhile.cs","lineNumber":69,"sourceCode":"        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeWhile(observer, predicate)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> TakeWhile<TSource>(IAsyncObserver<TSource> observer, Func<TSource, bool> predicate)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return Create<TSource>(\n                async x =>\n                {\n                    bool b;\n\n                    try\n                    {\n                        b = predicate(x);\n                    }\n                    catch (Exception ex)\n                    {\n                        await observer.OnErrorAsync(ex).ConfigureAwait(false);\n                        return;\n                    }\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeWhile.cs#L51-L87","documentation":"AsyncObserver.TakeWhile (the observer-factory overload taking Func<TSource, bool>) throws ArgumentNullException when the downstream IAsyncObserver<TSource> is null. Observer factories in System.Reactive.Async validate both observer and predicate because the returned observer wraps the downstream observer and forwards emissions to it.","triggerScenarios":"Calling AsyncObserver.TakeWhile(observer, predicate) with a null observer, typically when composing custom subscription pipelines where the downstream observer comes from a factory or field that is null.","commonSituations":"Custom operator authoring where the observer parameter of a composed SubscribeSafeAsync callback was not propagated; test harnesses constructing observers manually.","solutions":["Pass the actual downstream observer received from the subscription callback instead of null.","Null-check the observer before composing the operator chain and fail early with a clear message.","When piping, always thread the observer argument through the lambda: (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeWhile(observer, predicate))."],"exampleFix":"// before\nsource.SubscribeSafeAsync(AsyncObserver.TakeWhile(null, x => x < 10));\n// after\nawait source.SubscribeSafeAsync(AsyncObserver.TakeWhile(observer, x => x < 10));","handlingStrategy":"validation","validationCode":"if (observer is null) throw new ArgumentNullException(nameof(observer));\nvar obs = AsyncObserver.TakeWhile(observer, predicate);","typeGuard":"bool IsValidObserver<T>(IAsyncObserver<T>? o) => o is not null;","tryCatchPattern":"try { var obs = AsyncObserver.TakeWhile(observer, pred); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* propagate the real observer */ }","preventionTips":["Always forward the observer parameter in composition lambdas","Null-check observers in custom operator helpers","Never construct observers before the subscription provides one"],"tags":["csharp","null-argument","async-rx","observer"],"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"}