{"record":{"id":"02296d1e5b91d875","repo":"dotnet/reactive","slug":"argument-cannot-be-null-parameter-name-observer-02296d","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/FirstOrDefault.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.FirstOrDefault(observer, predicate)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> FirstOrDefault<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.OnNextAsync(default).ConfigureAwait(false);\n                    await observer.OnCompletedAsync().ConfigureAwait(false);\n                }\n            );\n        }\n\n        public static IAsyncObserver<TSource> FirstOrDefault<TSource>(IAsyncObserver<TSource> observer, Func<TSource, bool> predicate)\n        {","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/FirstOrDefault.cs#L37-L73","documentation":"AsyncObserver.FirstOrDefault<TSource>(IAsyncObserver<TSource>) (FirstOrDefault.cs:55) throws ArgumentNullException when the observer is null. This observer-level operator wraps the observer so the first element is echoed and then completes immediately; without an observer there is nothing to forward to, so the library fails fast at construction.","triggerScenarios":"Calling AsyncObserver.FirstOrDefault(null) directly, or from custom operator code where the IAsyncObserver passed into your Subscribe lambda was not forwarded (e.g. captured field still null).","commonSituations":"Hand-rolled operators that forget to pass the incoming observer down to AsyncObserver factories; test harnesses constructing observer chains out of order; wrapper classes lazily creating the inner observer after the operator call.","solutions":["Forward the actual observer argument you received in the subscription delegate: static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.FirstOrDefault(observer)).","If you must hold the observer in a field, initialize it before composing the operator.","Add ThrowIfNull on the observer at the top of your custom operator for a clearer failure."],"exampleFix":"// before\nIAsyncObserver<int> _inner; // not yet assigned\nvar op = AsyncObserver.FirstOrDefault(_inner);\n\n// after\nreturn Create<int>(static (source, observer) =>\n    source.SubscribeSafeAsync(AsyncObserver.FirstOrDefault(observer)));","handlingStrategy":"validation","validationCode":"if (observer is null) throw new ArgumentNullException(nameof(observer));\nvar op = AsyncObserver.FirstOrDefault(observer);","typeGuard":"static bool HasObserver<T>(IAsyncObserver<T>? o) => o is not null;","tryCatchPattern":"try { var op = AsyncObserver.FirstOrDefault(observer); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* fix observer wiring */ }","preventionTips":["In custom operators always forward the incoming observer parameter","Avoid lazy observer initialization in fields","Compose observer chains inside the Create/Subscribe lambda where the observer is in scope"],"tags":["argumentnull","observer","asyncrx","firstordefault"],"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"}