{"record":{"id":"ca0eab046361e7d8","repo":"dotnet/reactive","slug":"argument-cannot-be-null-parameter-name-predicate","errorCode":null,"errorMessage":"Argument cannot be null (Parameter name: predicate)","messagePattern":"Argument cannot be null \\(Parameter name: predicate\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/First.cs","lineNumber":76,"sourceCode":"                async x =>\n                {\n                    await observer.OnNextAsync(x).ConfigureAwait(false);\n                    await observer.OnCompletedAsync().ConfigureAwait(false);\n                },\n                observer.OnErrorAsync,\n                async () =>\n                {\n                    await observer.OnErrorAsync(new InvalidOperationException(\"The sequence is empty.\")).ConfigureAwait(false);\n                }\n            );\n        }\n\n        public static IAsyncObserver<TSource> First<TSource>(IAsyncObserver<TSource> observer, Func<TSource, bool> predicate)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return Where(First(observer), predicate);\n        }\n\n        public static IAsyncObserver<TSource> First<TSource>(IAsyncObserver<TSource> observer, Func<TSource, ValueTask<bool>> predicate)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return Where(First(observer), predicate);\n        }\n    }\n}\n","sourceCodeStart":58,"sourceCodeEnd":92,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/First.cs#L58-L92","documentation":"AsyncObserver.First (the observer-level overload taking a sync predicate) throws ArgumentNullException when the predicate delegate is null. The AsyncRx operators validate all delegate arguments up front because a null predicate would only explode later, mid-stream, when the first element arrives. Failing fast at operator-construction time keeps the error at the call site where it can be fixed.","triggerScenarios":"Calling AsyncObserver.First(observer, predicate) (Func<TSource, bool> overload) with a null predicate, typically via FirstAsync(source, (Func<TSource,bool>)null) or by passing a nullable delegate variable that was never assigned.","commonSituations":"Conditional predicate construction (build predicate only if a filter flag is set) that leaves the variable null; casting/reflective invocation passing null for the optional filter; refactoring where a lambda was moved out and the variable lost its initializer.","solutions":["Pass a real predicate; for 'no filtering' semantics call the First overload without a predicate instead of passing null.","If the predicate is computed, default it before the call: predicate ?? (_ => true) only when filtering is intentionally bypassed.","Check the caller (FirstAsync) at First.cs and ensure the value forwarded is a non-null delegate; guard at your own boundary with ArgumentNullException.ThrowIfNull(predicate)."],"exampleFix":"// before\nFunc<int, bool> pred = condition ? x => x > 0 : null;\nvar obs = AsyncObserver.First(observer, pred);\n\n// after\nvar obs = condition ? AsyncObserver.First(observer, (int x) => x > 0) : AsyncObserver.First(observer);","handlingStrategy":"validation","validationCode":"if (predicate is null) throw new ArgumentNullException(nameof(predicate));\nvar obs = AsyncObserver.First(observer, predicate);","typeGuard":"static bool IsValid<T>(Func<T, bool>? p) => p is not null;","tryCatchPattern":"try { var obs = AsyncObserver.First(observer, predicate); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"predicate\") { /* supply default predicate or log */ }","preventionTips":["Never store delegates in nullable fields consumed by Rx operators","Use the parameterless-predicate overload for unfiltered semantics","Enable nullable reference types so NRT warnings flag null delegate paths"],"tags":["argumentnull","predicate","asyncrx","first"],"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"}