dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'source')
Error message
Value cannot be null. (Parameter 'source')
What it means
ArgumentNullException thrown by the LongCount extension operator when the source async observable is null. Like other operators, LongCount validates arguments eagerly at call time, before the counting pipeline is created, so failures surface immediately with the parameter name.
Solutions
- Ensure the source passed to LongCount is a non-null IAsyncObservable<T>
- Substitute AsyncObservable.Empty<T>() when null semantically means 'no items' (LongCount would then return 0)
- Fix the upstream producer returning null
- Add an explicit null guard with a domain-specific exception before calling
Example fix
// before var count = await source.LongCountAsync(); // after var count = await (source ?? AsyncObservable.Empty<int>()).LongCountAsync();
Defensive patterns
Strategy: validation
Validate before calling
if (source is null) throw new ArgumentNullException(nameof(source)); // or semantically-correct fallback: var safeSource = source ?? AsyncObservable.Empty<TSource>();
Type guard
static bool IsObservableSet<T>(IAsyncObservable<T>? s) => s is not null;
Try / catch
try
{
var count = await source.LongCountAsync();
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
// source missing; treat count as 0 or surface config error
} Prevention
- Never return null from observable-producing methods; return Empty<T>()
- Validate sources at pipeline entry points before chaining operators
- Enable nullable reference types and address flow-analysis warnings on source variables
When it happens
Trigger: Calling AsyncObservable.LongCount(null) with or without a predicate — typically when the source expression (a factory, cache lookup, or field) evaluated to null.
Common situations: Passing a nullable observable field that was never initialized, a method returning null instead of an empty observable, or refactoring that broke source assignment.
Related errors
- Value cannot be null. (Parameter 'predicate')
- Value cannot be null. (Parameter 'source')
- Value cannot be null. (Parameter 'observer')
- Value cannot be null. (Parameter 'source')
- Value cannot be null. (Parameter 'observer')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/f36b080f361e494c.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/LongCount.cs:14
// 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<long> LongCount<TSource>(this IAsyncObservable<TSource> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
return Create<TSource, long>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.LongCount<TSource>(observer)));
}
public static IAsyncObservable<long> LongCount<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<long>.From(
source,
predicate,
static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.LongCount(observer, predicate)));
}
public static IAsyncObservable<long> LongCount<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)View on GitHub (pinned to 94b5d5ab91)