{"record":{"id":"6e7d3408b0ba3a62","repo":"dotnet/reactive","slug":"thrown-when-condition-is-null-argumentnullexception-param-6e7d34","errorCode":null,"errorMessage":"Thrown when condition is null (ArgumentNullException, param name: condition)","messagePattern":"Thrown when condition is null \\(ArgumentNullException, param name: condition\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Generate.cs","lineNumber":24,"sourceCode":"\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Generates a sequence by mimicking a for loop.\n        /// </summary>\n        /// <typeparam name=\"TState\">State type.</typeparam>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"initialState\">Initial state of the generator loop.</param>\n        /// <param name=\"condition\">Loop condition.</param>\n        /// <param name=\"iterate\">State update function to run after every iteration of the generator loop.</param>\n        /// <param name=\"resultSelector\">Result selector to compute resulting sequence elements.</param>\n        /// <returns>Sequence obtained by running the generator loop, yielding computed elements.</returns>\n        public static IEnumerable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, 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 GenerateCore(initialState, condition, iterate, resultSelector);\n        }\n\n        private static IEnumerable<TResult> GenerateCore<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)\n        {\n            for (var i = initialState; condition(i); i = iterate(i))\n            {\n                yield return resultSelector(i);\n            }\n        }\n    }\n}\n","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Generate.cs#L6-L42","documentation":"Generate builds an infinite/looping sequence driven by a condition predicate. Because condition is invoked during iteration, a null predicate would fail obscurely inside the iterator; the library validates it eagerly and throws ArgumentNullException with param name 'condition'.","triggerScenarios":"Calling Generate(initialState, null, iterate, resultSelector) — e.g. passing a Func<TState,bool> variable or expression-built predicate that resolved to null.","commonSituations":"Condition supplied by configuration or reflection that was not registered; ternary/conditional code paths assigning the predicate only in one branch; refactorings replacing a lambda with a nullable method group.","solutions":["Supply a concrete predicate, e.g. state => state < 10.","Coalesce to a sane default: condition ?? (_ => false) for a never-iterating generator.","Fix whatever was supposed to construct/inject the predicate."],"exampleFix":"// before\nvar seq = GeneratorEx.Generate(0, cond, x => x + 1, x => x); // cond is null\n// after\nvar seq = GeneratorEx.Generate(0, cond ?? (_ => false), x => x + 1, x => x);","handlingStrategy":"validation","validationCode":"if (condition is null) throw new ArgumentException(\"condition predicate is required\");","typeGuard":"bool HasPredicate<TState>(Func<TState, bool>? c) => c is not null;","tryCatchPattern":"try { var seq = GeneratorEx.Generate(state, condition, iterate, selector); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"condition\") { log.Error(\"generator condition missing\"); }","preventionTips":["Provide terminating predicates explicitly (e.g. s => s < limit).","Validate all delegate arguments at the boundary of builder methods.","Prefer lambdas over nullable delegate variables when composing generators."],"tags":["null-argument","linq","generator"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}