{"record":{"id":"ae254ab352da5b0b","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-condition","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'condition')","messagePattern":"Value cannot be null\\. \\(Parameter 'condition'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs","lineNumber":41,"sourceCode":"\n        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector, IAsyncScheduler scheduler)\n        {\n            if (condition == null)\n                throw new ArgumentNullException(nameof(condition));\n            if (iterate == null)\n                throw new ArgumentNullException(nameof(iterate));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            return Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector, scheduler));\n        }\n\n        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, ValueTask<bool>> condition, Func<TState, ValueTask<TState>> iterate, Func<TState, ValueTask<TResult>> resultSelector)\n        {\n            if (condition == null)\n                throw new ArgumentNullException(nameof(condition));\n            if (iterate == null)\n                throw new ArgumentNullException(nameof(iterate));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n\n            return Create<TResult>(observer => AsyncObserver.Generate(observer, initialState, condition, iterate, resultSelector));\n        }\n\n        public static IAsyncObservable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, ValueTask<bool>> condition, Func<TState, ValueTask<TState>> iterate, Func<TState, ValueTask<TResult>> resultSelector, IAsyncScheduler scheduler)\n        {\n            if (condition == null)\n                throw new ArgumentNullException(nameof(condition));\n            if (iterate == null)\n                throw new ArgumentNullException(nameof(iterate));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Generate.cs#L23-L59","documentation":"The scheduler-less overload Generate<TState,TResult>(initialState, condition, iterate, resultSelector) throws ArgumentNullException when the condition delegate is null. Generate evaluates condition before every iteration, so a null predicate would be unobservable until runtime; the library validates eagerly. This is fail-fast argument validation of the caller's delegates.","triggerScenarios":"Calling Generate(initialState, null, iterate, resultSelector) — the 4-argument ValueTask-based overload at Generate.cs:41 ('if (condition == null) throw new ArgumentNullException(nameof(condition))').","commonSituations":"Building the predicate dynamically (e.g. from a filter expression) and the expression compiler returning null; copying a call from another overload and forgetting the condition argument; DI-injected predicate delegate not registered.","solutions":["Supply a valid Func<TState, ValueTask<bool>> condition delegate, e.g. state => new ValueTask<bool>(state < limit).","If the condition comes from a variable/expression, assert it is non-null before calling Generate and fix the producer.","Use a helper to wrap a sync predicate when only Func<TState,bool> is available: s => new ValueTask<bool>(syncPred(s))."],"exampleFix":"// before\nvar xs = AsyncObservable.Generate(0, (Func<int, ValueTask<bool>>)null, i => new ValueTask<int>(i + 1), i => new ValueTask<int>(i));\n// after\nvar xs = AsyncObservable.Generate(0, i => new ValueTask<bool>(i < 10), i => new ValueTask<int>(i + 1), i => new ValueTask<int>(i));","handlingStrategy":"validation","validationCode":"if (condition is null) condition = s => new ValueTask<bool>(true); // or throw early\nvar xs = AsyncObservable.Generate(initialState, condition, iterate, resultSelector);","typeGuard":"static bool IsValidCondition<TState>(Func<TState, ValueTask<bool>> c) => c is not null;","tryCatchPattern":"try { var xs = AsyncObservable.Generate(state, cond, iter, sel); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"condition\") { /* supply a default predicate and retry */ }","preventionTips":["Never rely on a 'null means infinite' convention — pass _ => new ValueTask<bool>(true) instead","Resolve delegates from DI/config with non-null defaults","Use NRT annotations on your own wrapper APIs"],"tags":["argument-null","condition","generate","asyncrx","predicate"],"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"}