{"record":{"id":"b3bbe038417b8ca7","repo":"dotnet/reactive","slug":"throw-new-argumentnullexception-nameof-predicate","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(predicate));","messagePattern":"throw new ArgumentNullException\\(nameof\\(predicate\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeWhile.cs","lineNumber":29,"sourceCode":"        public static IAsyncObservable<TSource> TakeWhile<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.TakeWhile(observer, predicate)));\n        }\n\n        public static IAsyncObservable<TSource> TakeWhile<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,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeWhile(observer, predicate)));\n        }\n\n        public static IAsyncObservable<TSource> TakeWhile<TSource>(this IAsyncObservable<TSource> source, Func<TSource, int, 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.TakeWhile(observer, predicate)));","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeWhile.cs#L11-L47","documentation":"The async TakeWhile(source, predicate) overload throws ArgumentNullException because the predicate argument was null. The Func<TSource, ValueTask<bool>> predicate drives per-element async filtering; the library validates it eagerly and throws with the parameter name 'predicate'. Both this and the source check sit at the top of the operator, so the failure is immediate and unambiguous.","triggerScenarios":"Calling AsyncObservable.TakeWhile(source, (Func<TSource, ValueTask<bool>>)null).","commonSituations":"An async predicate factory (e.g. building a ValueTask-returning lambda from rules) returned null; wrong overload resolution made the compiler pick the async overload while the caller supplied a null sync delegate variable; dynamic rule compilation failed silently upstream.","solutions":["Pass a non-null async predicate, e.g. async x => await CheckAsync(x).","If the predicate is optional/dynamic, default to a constant-true async predicate: async _ => true.","Check which overload you intended — a null sync Func passed where the ValueTask overload is selected still throws here."],"exampleFix":"// before\nFunc<int, ValueTask<bool>> pred = BuildAsyncPredicate(rules); // null on failure\nvar taken = source.TakeWhile(pred);\n// after\nvar taken = source.TakeWhile(pred ?? async _ => true);","handlingStrategy":"validation","validationCode":"if (predicate == null) predicate = static async _ => true;","typeGuard":"bool HasAsyncPredicate<T>(Func<T, ValueTask<bool>>? p) => p is not null;","tryCatchPattern":"try { var taken = source.TakeWhile(asyncPredicate); } catch (ArgumentNullException ex) when (ex.ParamName == \"predicate\") { var taken = source; /* no filtering */ }","preventionTips":["Default dynamically built async predicates to async _ => true when construction fails.","Confirm overload selection when mixing sync Func<bool> and ValueTask<bool> predicates.","Log and handle rule-compilation failures at the point they occur, not at pipeline composition."],"tags":["argument-null","predicate","takewhile","async"],"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"}