{"record":{"id":"49dbe22b5ce1f921","repo":"dotnet/reactive","slug":"predicate-takewhile","errorCode":null,"errorMessage":"predicate","messagePattern":"predicate","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeWhile.cs","lineNumber":16,"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> 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)));","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeWhile.cs#L1-L34","documentation":"The TakeWhile(source, predicate) operator throws ArgumentNullException because the predicate delegate argument was null. The predicate is invoked for every emitted element to decide whether to keep taking; the library rejects a null predicate at pipeline-construction time with the parameter name 'predicate' in the message. A valid source does not prevent this throw.","triggerScenarios":"Calling AsyncObservable.TakeWhile(source, null) where the Func<TSource, bool> argument is null.","commonSituations":"Building the predicate dynamically (e.g. from a filter expression) and the builder returned null; a config-driven filter string that failed to compile to a delegate; passing a method group that was removed/renamed during refactoring and now resolves via a null variable.","solutions":["Pass a concrete predicate, e.g. x => x > 0, or a named method group.","If the predicate is built dynamically, fall back to a always-true predicate (x => true) when construction fails.","Validate the delegate before composing the pipeline if it comes from external configuration."],"exampleFix":"// before\nFunc<int, bool> pred = BuildPredicate(cfg); // may return null\nvar taken = source.TakeWhile(pred);\n// after\nvar taken = source.TakeWhile(pred ?? (_ => true));","handlingStrategy":"validation","validationCode":"if (predicate == null) predicate = static _ => true;","typeGuard":"bool HasPredicate<T>(Func<T, bool>? p) => p is not null;","tryCatchPattern":"try { var taken = source.TakeWhile(predicate); } catch (ArgumentNullException ex) when (ex.ParamName == \"predicate\") { var taken = source; /* no filtering */ }","preventionTips":["When predicates are built dynamically, always return a fallback (x => true) instead of null.","Validate config-driven filter compilation before composing the query.","Prefer named method groups over delegate variables so nulls cannot be introduced."],"tags":["argument-null","predicate","takewhile","reactive-extensions"],"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"}