{"record":{"id":"382d69aae134e231","repo":"dotnet/reactive","slug":"nameof-source-elementat","errorCode":null,"errorMessage":"nameof(source)","messagePattern":"nameof\\(source\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAt.cs","lineNumber":14,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> ElementAt<TSource>(this IAsyncObservable<TSource> source, int index)\n        {\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));","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAt.cs#L1-L32","documentation":"Thrown by ElementAt(source, index) when the source IAsyncObservable is null. ElementAt emits the item at a zero-based position, and the library validates inputs at composition time with ArgumentNullException. The message names source.","triggerScenarios":"Calling AsyncObservable.ElementAt(null, index) — a null stream from a failed factory, an unassigned member, or swapped arguments where index landed in the source slot.","commonSituations":"Chaining off a method that returned null instead of an empty observable; dynamic query construction where an optional stream is absent; tests defaulting source to null.","solutions":["Pass a non-null IAsyncObservable<TSource> as the first argument","Coalesce with a fallback: source ?? AsyncObservable.Empty<TSource>()","Trace the upstream factory/operator call that produced the null source","Verify argument order: source first, index second"],"exampleFix":"// before\nvar item = AsyncObservable.ElementAt(GetStream(), 2); // GetStream() returned null -> throws\n// after\nvar src = GetStream();\nif (src == null) throw new InvalidOperationException(\"no stream configured\");\nvar item = AsyncObservable.ElementAt(src, 2);","handlingStrategy":"validation","validationCode":"if (source == null) throw new InvalidOperationException(\"ElementAt requires a non-null source\");\nvar item = AsyncObservable.ElementAt(source, index);","typeGuard":"bool IsStreamValid<TSource>(IAsyncObservable<TSource> s) => s is not null;","tryCatchPattern":"try\n{\n    var item = AsyncObservable.ElementAt(source, index);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    // source was null; use a fallback stream or propagate a domain error\n    item = AsyncObservable.ElementAt(AsyncObservable.Empty<TSource>(), index);\n}","preventionTips":["Make stream-producing methods return empty observables instead of null","Coalesce null streams at pipeline entry points","Enable nullable reference types to catch null sources at compile time","Keep (source, index) argument order; verify when refactoring"],"tags":["argument-null","asyncrx","elementat-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"}