{"record":{"id":"137814f9cfcb8d1c","repo":"dotnet/reactive","slug":"thrown-when-resultselector-is-null-argumentnullexception","errorCode":null,"errorMessage":"Thrown when resultSelector is null (ArgumentNullException, param name: resultSelector)","messagePattern":"Thrown when resultSelector is null \\(ArgumentNullException, param name: resultSelector\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/For.cs","lineNumber":28,"sourceCode":"    {\n        /// <summary>\n        /// Generates a sequence by enumerating a source sequence, mapping its elements on result sequences, and concatenating\n        /// those sequences.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"resultSelector\">Result selector to evaluate for each iteration over the source.</param>\n        /// <returns>\n        /// Sequence concatenating the inner sequences that result from evaluating the result selector on elements from\n        /// the source.\n        /// </returns>\n        public static IEnumerable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (resultSelector == null)\n                throw new ArgumentNullException(nameof(resultSelector));\n\n            return source.Select(resultSelector).Concat();\n        }\n    }\n}\n","sourceCodeStart":10,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/For.cs#L10-L34","documentation":"For (a foreach-style bind) maps each source element onto a result sequence via resultSelector; null is rejected because the per-element sequence could not be produced. Fix: pass a non-null Func<TSource, IEnumerable<TResult>>.","triggerScenarios":"Calling For(source, null), e.g. the selector obtained from a nullable strategy/injection point.","commonSituations":"Missing DI registration for the projection delegate; passing obj?.Selector which evaluates to null; a factory that returns null for a fallback case.","solutions":["Supply a valid Func<TSource, IEnumerable<TResult>>; use _ => Enumerable.Empty<TResult>() to map to nothing.","Verify the delegate is initialized before query execution.","Add a default at the delegate acquisition point."],"exampleFix":"// before\nvar result = For(source, projector); // projector may be null\n// after\nvar result = For(source, projector ?? (_ => Enumerable.Empty<TResult>()));","handlingStrategy":"validation","validationCode":"if (resultSelector is null) throw new ArgumentNullException(nameof(resultSelector));\nFor(source, resultSelector ?? (_ => Enumerable.Empty<TResult>()));","typeGuard":"bool HasSelector<TSrc,TRes>(Func<TSrc,IEnumerable<TRes>> f) => f is not null;","tryCatchPattern":"try { var q = For(source, sel); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"resultSelector\") { /* default projection */ }","preventionTips":["Initialize projection delegates before query construction.","Provide empty-mapping defaults for optional selectors.","Check DI/factory wiring that produces the selector.","Avoid null-conditional delegate passing."],"tags":["null-argument","argument-validation","delegate"],"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"}