dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'predicate')

Error message

Value cannot be null. (Parameter 'predicate')

What it means

The All(source, predicate) overload throws ArgumentNullException when the predicate delegate is null. Rx validates arguments eagerly in the public wrapper before delegating to s_impl.All, so a null predicate fails immediately at the call site. The doc comment explicitly lists predicate as throwing ArgumentNullException.

Solutions

  1. Pass a real predicate, e.g. `source.All(x => x > 0)`; if you truly want 'all pass', use `x => true`.
  2. Guard optional predicates: `source.All(predicate ?? (_ => true))`.
  3. Fix the rules/config layer so it always yields a compiled Func<TSource,bool> instead of null.
  4. Add a unit test covering the null-predicate path of your pipeline builder.

Example fix

// before
Func<int, bool> predicate = rules.GetPredicate(); // may be null
var all = source.All(predicate);

// after
var all = source.All(rules.GetPredicate() ?? (_ => true));
Defensive patterns

Strategy: validation

Validate before calling

if (source is null || predicate is null)
    throw new InvalidOperationException("All(source, predicate) requires non-null arguments");
var all = source.All(predicate);

Type guard

bool IsValidPredicate<TSource>(Func<TSource,bool>? p) => p is not null;

Try / catch

try { var all = source.All(predicate); }
catch (ArgumentNullException ex) when (ex.ParamName == "predicate")
{
    var all = source.All(_ => true); // treat missing predicate as always-true
}

Prevention

When it happens

Trigger: Calling `source.All(null)` directly, or passing a Func field/property/returned delegate that was never initialized, e.g. `Func<int,bool> test = null; source.All(test);` — throws at Observable.Aggregates.cs line ~123.

Common situations: Building operator pipelines dynamically where the predicate comes from configuration or a rules engine and no rule matched; a helper method with an optional predicate parameter that is forwarded without a default; reflection-created delegates that ended up null.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Aggregates.cs:123

        /// <summary>
        /// Determines whether all elements of an observable sequence satisfy a condition.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">An observable sequence whose elements to apply the predicate to.</param>
        /// <param name="predicate">A function to test each element for a condition.</param>
        /// <returns>An observable sequence containing a single element determining whether all elements in the source sequence pass the test in the specified predicate.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</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<bool> All<TSource>(this IObservable<TSource> source, Func<TSource, bool> predicate)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            return s_impl.All(source, predicate);
        }

        #endregion

        #region + Any +

        /// <summary>
        /// Determines whether an observable sequence contains any elements.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">An observable sequence to check for non-emptiness.</param>
        /// <returns>An observable sequence containing a single element determining whether the source sequence contains any elements.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</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<bool> Any<TSource>(this IObservable<TSource> source)

View on GitHub (pinned to 94b5d5ab91)