{"record":{"id":"27c038a20d03f9b9","repo":"dotnet/reactive","slug":"argument-cannot-be-null-parameter-name-observer","errorCode":null,"errorMessage":"Argument cannot be null (Parameter name: observer)","messagePattern":"Argument cannot be null \\(Parameter name: observer\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/First.cs","lineNumber":55,"sourceCode":"        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return Create(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.First(observer, predicate)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> First<TSource>(IAsyncObserver<TSource> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            return Create<TSource>(\n                async x =>\n                {\n                    await observer.OnNextAsync(x).ConfigureAwait(false);\n                    await observer.OnCompletedAsync().ConfigureAwait(false);\n                },\n                observer.OnErrorAsync,\n                async () =>\n                {\n                    await observer.OnErrorAsync(new InvalidOperationException(\"The sequence is empty.\")).ConfigureAwait(false);\n                }\n            );\n        }\n\n        public static IAsyncObserver<TSource> First<TSource>(IAsyncObserver<TSource> observer, Func<TSource, bool> predicate)\n        {\n            if (observer == null)","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/First.cs#L37-L73","documentation":"ArgumentNullException (Parameter name: observer) thrown by AsyncObserver.First<TSource>(IAsyncObserver<TSource>) when the downstream observer is null. This factory builds the observer-side logic that takes the first element then completes; it validates the observer before wiring callbacks. It surfaces through First/FirstAsync when internals or custom subscriptions pass a null observer.","triggerScenarios":"Calling AsyncObserver.First(null) directly, or implementing a custom IAsyncObservable/operator that passes a null observer into AsyncObserver.First — typically from a broken SubscribeSafeAsync path or a custom Create overload.","commonSituations":"Custom operator development where the observer argument is forwarded from a caller that supplied null; unit tests invoking AsyncObserver factories with null arguments.","solutions":["In custom operators, validate the observer with ArgumentNullException.ThrowIfNull before calling AsyncObserver.First","Ensure the caller (Create/SubscribeSafeAsync path) always supplies a non-null observer","When testing, pass a stub/test observer rather than null"],"exampleFix":"// before\npublic IAsyncObservable<T> MyOp(...) => ... AsyncObserver.First(observer); // observer may be null\n// after\npublic IAsyncObservable<T> MyOp(...)\n{\n    ArgumentNullException.ThrowIfNull(observer);\n    return ... AsyncObserver.First(observer);\n}","handlingStrategy":"validation","validationCode":"if (observer == null) throw new ArgumentNullException(nameof(observer));\nvar firstObserver = AsyncObserver.First(observer);","typeGuard":"bool HasObserver<T>(IAsyncObserver<T>? o) => o is not null;","tryCatchPattern":"try\n{\n    var firstObserver = AsyncObserver.First(observer);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == nameof(observer))\n{\n    throw new InvalidOperationException(\"Downstream observer missing\", ex);\n}","preventionTips":["In custom operators, always validate the observer before composing","Never construct observers conditionally such that they can be null","Cover custom operator Subscribe paths with tests that pass a real observer"],"tags":["csharp","null-argument","observer","async-rx"],"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"}