dotnet/reactive · error · ArgumentNullException
selector
Error message
selector
What it means
System.Reactive throws ArgumentNullException when the selector delegate passed to Average<TSource>(IObservable<TSource>, Func<TSource,int>) is null. Like all Rx aggregate operators, the selector is validated immediately so failures surface at the call site rather than during subscription.
Solutions
- Supply a valid selector lambda, e.g. x => (int)x.Score
- Default the selector before the call when it may be absent
- Verify argument order in the call is (source, selector)
Example fix
// before var avg = xs.Average(config.Selector); // may be null // after Func<Item,int> selector = config.Selector ?? (x => x.Count); var avg = xs.Average(selector);
Defensive patterns
Strategy: validation
Validate before calling
if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector));
Type guard
bool HasSelector<TSource>(Func<TSource,int>? s) => s is not null;
Try / catch
try { var avg = xs.Average(sel); }
catch (ArgumentNullException ex) when (ex.ParamName == "selector") { var avg = xs.Average(x => (int)x.Default); } Prevention
- Validate resolved delegates before use
- Avoid conditional expressions that can yield a null branch
- Keep (source, selector) argument order consistent in call sites
When it happens
Trigger: Passing a null Func<TSource,int> to Observable.Average, e.g. xs.Average((Func<MyType,int>)null) or xs.Average(condition ? selectorA : null) where the condition is false.
Common situations: Optional projections resolved from DI or settings; conditional expression trees where one branch is null; passing the result of a lookup that found no delegate.
Related errors
- source
- Value cannot be null. (Parameter 'elementSelector')
- observer
- Value cannot be null. (Parameter 'defaultSource')
- Value cannot be null. (Parameter 'source')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/3268a270f37493b7.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Aggregates.cs:461
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <returns>An observable sequence containing a single element with the average of the sequence of values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
/// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
/// <exception cref="OverflowException">(Asynchronous) The sum of the projected values for the elements in the source sequence is larger than <see cref="long.MaxValue"/>.</exception>
/// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
public static IObservable<double> Average<TSource>(this IObservable<TSource> source, Func<TSource, int> selector)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (selector == null)
{
throw new ArgumentNullException(nameof(selector));
}
return s_impl.Average(source, selector);
}
/// <summary>
/// Computes the average of an observable sequence of <see cref="long" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <returns>An observable sequence containing a single element with the average of the sequence of values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
/// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
/// <exception cref="OverflowException">(Asynchronous) The sum of the projected values for the elements in the source sequence is larger than <see cref="long.MaxValue"/>.</exception>
/// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
public static IObservable<double> Average<TSource>(this IObservable<TSource> source, Func<TSource, long> selector)
{View on GitHub (pinned to 94b5d5ab91)