dotnet/reactive · error · ArgumentNullException
Thrown when resultSelector is null (ArgumentNullException…
Error message
Thrown when resultSelector is null (ArgumentNullException, param name: resultSelector)
What it means
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>>.
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.
Example fix
// before var result = For(source, projector); // projector may be null // after var result = For(source, projector ?? (_ => Enumerable.Empty<TResult>()));
Defensive patterns
Strategy: validation
Validate before calling
if (resultSelector is null) throw new ArgumentNullException(nameof(resultSelector)); For(source, resultSelector ?? (_ => Enumerable.Empty<TResult>()));
Type guard
bool HasSelector<TSrc,TRes>(Func<TSrc,IEnumerable<TRes>> f) => f is not null;
Try / catch
try { var q = For(source, sel); }
catch (ArgumentNullException ex) when (ex.ParamName == "resultSelector") { /* default projection */ } Prevention
- 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.
When it happens
Trigger: Calling For(source, null), e.g. the selector obtained from a nullable strategy/injection point.
Common situations: Missing DI registration for the projection delegate; passing obj?.Selector which evaluates to null; a factory that returns null for a fallback case.
Related errors
- Thrown when condition is null (ArgumentNullException, param…
- Thrown when finallyAction is null (ArgumentNullException…
- Thrown when onNext is null (ArgumentNullException, param…
- Thrown when selector is null (ArgumentNullException, param…
- Value cannot be null. (Parameter 'handler')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/137814f9cfcb8d1c.
Report an issue: GitHub.
Appendix: source
Thrown at Ix.NET/Source/System.Interactive/System/Linq/Operators/For.cs:28
{
/// <summary>
/// Generates a sequence by enumerating a source sequence, mapping its elements on result sequences, and concatenating
/// those sequences.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <typeparam name="TResult">Result sequence element type.</typeparam>
/// <param name="source">Source sequence.</param>
/// <param name="resultSelector">Result selector to evaluate for each iteration over the source.</param>
/// <returns>
/// Sequence concatenating the inner sequences that result from evaluating the result selector on elements from
/// the source.
/// </returns>
public static IEnumerable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (resultSelector == null)
throw new ArgumentNullException(nameof(resultSelector));
return source.Select(resultSelector).Concat();
}
}
}
View on GitHub (pinned to 94b5d5ab91)