dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'selector')
Error message
Value cannot be null. (Parameter 'selector')
What it means
The decimal-selector Average overload throws ArgumentNullException("selector") when the projection function passed is null. The source check has already passed; the selector delegate is validated second.
Solutions
- Supply a concrete selector lambda instead of the null delegate.
- Guard: if (selector == null) selector = x => (decimal)x; or use a default projection.
- Check where the delegate comes from (config, DI, dictionary) and fix the null assignment.
Example fix
// before Func<Order, decimal> sel = GetSelector(); // returns null var avg = orders.Average(sel); // after var avg = orders.Average(o => o.Total);
Defensive patterns
Strategy: validation
Validate before calling
if (selector is null) selector = static o => o.Total; // or fail fast var avg = orders.Average(selector);
Type guard
static bool HasSelector<T>(Func<T, decimal>? f) => f is not null;
Try / catch
try { var avg = orders.Average(selector); }
catch (ArgumentNullException ex) when (ex.ParamName == "selector") { /* install default selector */ } Prevention
- Never store selectors in nullable fields without defaults.
- Validate delegate registrations (config/DI/registry) at load time.
- Prefer inline lambdas over nullable delegate variables for fixed projections.
When it happens
Trigger: Calling observable.Average((Func<T, decimal>)someField) where someField is a null delegate — e.g. an uninitialized selector field or a null entry read from configuration/DI.
Common situations: Dynamic selector selection (strategy pattern) where the default branch forgets to assign a projection; reflection-built pipelines passing null delegates.
Related errors
- subjectSelector
- keySelector
- Value cannot be null. (Parameter 'keySelector')
- Value cannot be null. (Parameter 'comparer')
- source
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/84b31c593b27bac3.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Aggregates.cs:385
/// </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="decimal.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<decimal> Average<TSource>(this IObservable<TSource> source, Func<TSource, decimal> 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="double" /> 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>
/// <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, double> selector)
{
if (source == null)View on GitHub (pinned to 94b5d5ab91)