{"record":{"id":"c4aa2acbae9652b0","repo":"dotnet/reactive","slug":"nameof-observer-elementat","errorCode":null,"errorMessage":"nameof(observer)","messagePattern":"nameof\\(observer\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAt.cs","lineNumber":30,"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.ElementAt(observer, index)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> ElementAt<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.OnErrorAsync(new ArgumentOutOfRangeException(\"The element at the specified index was not found.\")).ConfigureAwait(false);\n                }\n            );","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAt.cs#L12-L48","documentation":"This is an ArgumentNullException thrown by AsyncObserver.ElementAt when the observer argument is null. The library validates its arguments up front because the operator factory builds an observer wrapper that would otherwise fail later with a confusing NullReferenceException inside the subscription pipeline. Passing a null IAsyncObserver is always a programming error, so the check throws immediately and synchronously.","triggerScenarios":"Calling AsyncObserver.ElementAt<TSource>(null, index) with a null observer, e.g. when the observer came from an uninitialized field, a failed factory call, or a mis-ordered method chain that returned null.","commonSituations":"Wiring custom async Rx pipelines by hand where an observer variable is never assigned; refactoring subscription code and accidentally passing null; conditional logic that builds an observer but skips assignment on some path.","solutions":["Ensure a non-null IAsyncObserver<TSource> is passed, e.g. obtained from AsyncObserver.Create or a downstream operator","Check the code path that produced the observer for assignments that can yield null","If the observer may legitimately be absent, gate the call: if (observer != null) AsyncObserver.ElementAt(observer, index)"],"exampleFix":"// before\nvar op = AsyncObserver.ElementAt(observer, 2); // observer is null\n// after\nif (observer == null) throw new InvalidOperationException(\"observer not initialized\");\nvar op = AsyncObserver.ElementAt(observer, 2);","handlingStrategy":"validation","validationCode":"if (observer is null) throw new ArgumentNullException(nameof(observer));\nvar op = AsyncObserver.ElementAt(observer, index);","typeGuard":"bool IsValidObserver<TSource>(IAsyncObserver<TSource> o) => o is not null;","tryCatchPattern":"try { var op = AsyncObserver.ElementAt(observer, index); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* supply a real observer */ }","preventionTips":["Never build operators on observers that can be null; use AsyncObserver.Create to obtain one","Enable nullable reference types so null observer flows are caught at compile time","Assert non-null arguments at pipeline composition boundaries"],"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"}