dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'predicate')

Error message

Value cannot be null. (Parameter 'predicate')

What it means

Count(source, Func<TSource,bool>) throws ArgumentNullException because the synchronous predicate delegate is null. The predicate is applied per element before counting; without it the operator cannot decide which elements to include. It is validated eagerly at the Count() call site.

Solutions

  1. Pass a concrete predicate, e.g. Count(x => x > 0).
  2. If no filtering is needed, call the parameterless Count() overload instead of passing null.
  3. Default the predicate variable to a constant-true lambda: _ => true.

Example fix

// before
Func<int, bool> pred = ConfigureFilter(); // may return null
var n = await source.Count(pred);
// after
var n = await source.Count(ConfigureFilter() ?? (_ => true));
Defensive patterns

Strategy: validation

Validate before calling

if (predicate is null) throw new ArgumentNullException(nameof(predicate));

Type guard

static bool HasPredicate<T>(Func<T,bool>? p) => p is not null;

Try / catch

try { var n = await source.Count(pred); } catch (ArgumentNullException ex) when (ex.ParamName == "predicate") { pred = _ => true; }

Prevention

When it happens

Trigger: Passing null as the predicate: e.g. a delegate variable that was never assigned, a conditional lambda expression that evaluated to null, or a method group on a null object reference.

Common situations: Storing predicates in fields/config for dynamic filtering where the filter was never set; DI-injected filter functions defaulting to null; overloading confusion where the async predicate overload was intended but null was passed.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/1f8e700331bcc902. Report an issue: GitHub.

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Count.cs:24

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<int> Count<TSource>(this IAsyncObservable<TSource> source)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            return Create<TSource, int>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Count<TSource>(observer)));
        }

        public static IAsyncObservable<int> Count<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (predicate == null)
                throw new ArgumentNullException(nameof(predicate));

            return CreateAsyncObservable<int>.From(
                source,
                predicate,
                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Count(observer, predicate)));
        }

        public static IAsyncObservable<int> Count<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (predicate == null)
                throw new ArgumentNullException(nameof(predicate));

            return CreateAsyncObservable<int>.From(
                source,
                predicate,
                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Count(observer, predicate)));

View on GitHub (pinned to 94b5d5ab91)