{"record":{"id":"cceb10ca183253af","repo":"dotnet/reactive","slug":"argumentnullexception-nameof-source-where","errorCode":null,"errorMessage":"ArgumentNullException(nameof(source))","messagePattern":"ArgumentNullException\\(nameof\\(source\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Where.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> Where<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 CreateAsyncObservable<TSource>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Where(observer, predicate)));\n        }\n\n        public static IAsyncObservable<TSource> Where<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<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 CreateAsyncObservable<TSource>.From(\n                source,","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Where.cs#L1-L32","documentation":"The synchronous-predicate Where extension validates its inputs first. It throws ArgumentNullException with param name 'source' when the IAsyncObservable<TSource> being filtered is null. Null-checking before CreateAsyncObservable.From gives an immediate, accurately-named exception instead of a confusing failure at subscription.","triggerScenarios":"Chaining .Where(predicate) on an IAsyncObservable<TSource> variable/return value that is null — e.g. a factory method returned null or a field was never assigned.","commonSituations":"Source observables resolved from DI or a cache that returned null; method chains where an earlier operator returned null unexpectedly (custom operators); race where the field is assigned only after the query is composed.","solutions":["Ensure the source observable is created before filtering (e.g. AsyncObservable.Range(0, 10)).","Fix the producer that returned null instead of a non-null (possibly empty) observable.","Guard the chain: if (source == null) throw new InvalidOperationException(...) or coalesce with a default source."],"exampleFix":"// before\nIAsyncObservable<int> source = null;\nvar filtered = source.Where(x => x > 2); // throws\n// after\nvar source = AsyncObservable.Range(0, 10);\nvar filtered = source.Where(x => x > 2);","handlingStrategy":"validation","validationCode":"if (source == null)\n    throw new InvalidOperationException(\"source observable must not be null before Where\");","typeGuard":"bool HasSource<T>(IAsyncObservable<T> source) => source is not null;","tryCatchPattern":"try\n{\n    var filtered = source.Where(x => x > 2);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    // substitute AsyncObservable.Empty<T>() or report the null-producing factory\n}","preventionTips":["Never let factory methods return null observables; return AsyncObservable.Empty<T>().","Compose queries only after async source initialization completes.","Enable nullable reference types so null sources are caught at compile time."],"tags":["argument-null","linq-operator","async-rx","where"],"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"}