{"record":{"id":"c8f5e8b90d492796","repo":"dotnet/reactive","slug":"argument-cannot-be-null-parameter-name-source","errorCode":null,"errorMessage":"Argument cannot be null (Parameter name: source)","messagePattern":"Argument cannot be null \\(Parameter name: source\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/FirstOrDefault.cs","lineNumber":18,"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> FirstOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source) => FirstOrDefault(source);\n        public static IAsyncObservable<TSource> FirstOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => FirstOrDefault(source, predicate);\n        public static IAsyncObservable<TSource> FirstOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate) => FirstOrDefault(source, predicate);\n\n        public static IAsyncObservable<TSource> FirstOrDefault<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.FirstOrDefault(observer)));\n        }\n\n        public static IAsyncObservable<TSource> FirstOrDefault<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)\n        {\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        public static IAsyncObservable<TSource> FirstOrDefault<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/FirstOrDefault.cs#L1-L36","documentation":"FirstOrDefault<TSource>(IAsyncObservable<TSource>) (the no-predicate overload) throws ArgumentNullException when source is null. AsyncRx's operator API is extension-method based; a null source would cause a NullReferenceException only at Subscribe time, so the library guards eagerly and names the offending parameter. This keeps the stack trace at your call site.","triggerScenarios":"Calling source.FirstOrDefault() / FirstOrDefaultAsync(source) where the IAsyncObservable<TSource> variable is null — e.g. a factory or dictionary lookup returned null before the extension call.","commonSituations":"Chained operator pipelines where an earlier operator unexpectedly returned null; config-driven source selection where the configured source key resolves to nothing; caching of observables that was never populated.","solutions":["Fix the upstream factory/lookup so it returns a non-null IAsyncObservable; throw your own descriptive exception if the source cannot be resolved.","Verify you are calling the operator on the observable itself (source.FirstOrDefaultAsync()) and not passing a null into a static helper.","Guard before the call: if (source == null) throw new InvalidOperationException(...)."],"exampleFix":"// before\nvar result = sources[name].FirstOrDefaultAsync(); // sources[name] may be null\n\n// after\nif (!sources.TryGetValue(name, out var src)) throw new KeyNotFoundException($\"source '{name}' missing\");\nvar result = src.FirstOrDefaultAsync();","handlingStrategy":"validation","validationCode":"if (source is null) throw new InvalidOperationException(\"source observable not resolved\");\nvar first = source.FirstOrDefaultAsync();","typeGuard":"static bool HasSource<T>(IAsyncObservable<T>? s) => s is not null;","tryCatchPattern":"try { var first = src.FirstOrDefaultAsync(); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* log and use fallback stream */ }","preventionTips":["Check every operator earlier in the chain for null-returning branches","Validate DI registrations resolve to non-null observables at startup","Prefer throwing descriptive exceptions at resolution time over null propagation"],"tags":["argumentnull","source","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"}