{"record":{"id":"6019b3cb694aa2a5","repo":"dotnet/reactive","slug":"nameof-observer-elementatordefault","errorCode":null,"errorMessage":"nameof(observer)","messagePattern":"nameof\\(observer\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAtOrDefault.cs","lineNumber":28,"sourceCode":"        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (index < 0)\n                throw new ArgumentOutOfRangeException(nameof(index));\n\n            return Create(\n                source,\n                index,\n                static (source, index, observer) => source.SubscribeSafeAsync(AsyncObserver.ElementAtOrDefault(observer, index)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> ElementAtOrDefault<TSource>(IAsyncObserver<TSource> observer, int index)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (index < 0)\n                throw new ArgumentOutOfRangeException(nameof(index));\n\n            return Create<TSource>(\n                async x =>\n                {\n                    if (index-- == 0)\n                    {\n                        await observer.OnNextAsync(x).ConfigureAwait(false);\n                        await observer.OnCompletedAsync().ConfigureAwait(false);\n                    }\n                },\n                observer.OnErrorAsync,\n                async () =>\n                {\n                    await observer.OnNextAsync(default).ConfigureAwait(false);\n                    await observer.OnCompletedAsync().ConfigureAwait(false);\n                }","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAtOrDefault.cs#L10-L46","documentation":"This is an ArgumentNullException thrown by AsyncObserver.ElementAtOrDefault when the observer argument is null. The factory guards its arguments so that a null observer fails fast at composition time rather than corrupting the subscription later. Null is never a valid observer here.","triggerScenarios":"Calling AsyncObserver.ElementAtOrDefault<TSource>(null, index); typical when the observer is the result of a conditional chain or an uninitialized member at pipeline-build time.","commonSituations":"Hand-assembling async observer pipelines in tests; passing the result of a method that can return null; ordering mistakes where the observer is created after the operator call.","solutions":["Pass a valid IAsyncObserver<TSource> (from AsyncObserver.Create or an upstream operator)","Trace why the observer variable was null and fix that construction path","Assert non-null in dev builds before composing the pipeline"],"exampleFix":"// before\nvar op = AsyncObserver.ElementAtOrDefault(observer, 1); // observer null\n// after\nobserver ??= AsyncObserver.Create<TSource>(...);\nvar op = AsyncObserver.ElementAtOrDefault(observer, 1);","handlingStrategy":"validation","validationCode":"if (observer is null) throw new ArgumentNullException(nameof(observer));\nvar op = AsyncObserver.ElementAtOrDefault(observer, index);","typeGuard":"bool IsValidObserver<TSource>(IAsyncObserver<TSource> o) => o is not null;","tryCatchPattern":"try { var op = AsyncObserver.ElementAtOrDefault(observer, index); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* create/assign a real observer */ }","preventionTips":["Create observers via AsyncObserver.Create before composing operators","Avoid nullable observer members; initialize them eagerly","Unit-test pipeline builders for null-observer paths"],"tags":["argument-null","async-rx","operator"],"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"}