dotnet/reactive · error · ArgumentNullException

ArgumentNullException(nameof(predicate))

Error message

ArgumentNullException(nameof(predicate))

What it means

The synchronous-predicate Where extension throws ArgumentNullException with param name 'predicate' when the Func<TSource, bool> filter delegate is null. The check runs eagerly at composition time so the failure names the offending parameter.

Solutions

  1. Pass a valid predicate, e.g. x => x % 2 == 0.
  2. If the predicate is optional, substitute a default (x => true) before calling Where.
  3. Fix the filter-registry/builder so it never returns null (return a pass-through predicate instead).

Example fix

// before
Func<int, bool> pred = GetFilter(name); // may return null
var filtered = source.Where(pred);
// after
var pred = GetFilter(name) ?? (_ => true);
var filtered = source.Where(pred);
Defensive patterns

Strategy: validation

Validate before calling

if (predicate == null)
    predicate = _ => true; // pass-through default

Type guard

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

Try / catch

try
{
    var filtered = source.Where(predicate);
}
catch (ArgumentNullException ex) when (ex.ParamName == "predicate")
{
    // fall back to an unfiltered source or fix the filter registry
}

Prevention

When it happens

Trigger: Calling source.Where(null) with the sync predicate overload — a null lambda, an uninitialized Func variable, or a null result from a predicate-builder helper.

Common situations: Predicates selected from a dictionary of filters where the key lookup missed; optional predicate parameters defaulting to null; serialization/generation pipelines emitting null delegates.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Where.cs:16

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 

using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TSource> Where<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<TSource>.From(
                source,
                predicate,
                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Where(observer, predicate)));
        }

        public static IAsyncObservable<TSource> Where<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<TSource>.From(
                source,
                predicate,
                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Where(observer, predicate)));

View on GitHub (pinned to 94b5d5ab91)