dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'source')
Error message
Value cannot be null. (Parameter 'source')
What it means
The ToArray operator throws ArgumentNullException with message "Value cannot be null. (Parameter 'source')" when the source IAsyncObservable<TSource> is null. ToArray buffers all elements into a TSource[] emitted on completion, and buffering requires a valid source, validated at the operator call.
Solutions
- Ensure the source is non-null; substitute AsyncObservable.Empty<TSource>() for an intentionally empty stream.
- ThrowIfNull the source where the observable is produced so the failure points to the real defect.
- Fix the producing method/branch to always return a valid observable.
Example fix
// before var items = GetStream(); // null on failure var arr = await items.ToArray(); // after var items = GetStream() ?? AsyncObservable.Empty<int>(); var arr = await items.ToArray();
Defensive patterns
Strategy: validation
Validate before calling
if (source is null)
throw new InvalidOperationException("ToArray requires a non-null source observable."); Type guard
bool HasSource<T>(IAsyncObservable<T>? source) => source is not null;
Try / catch
try
{
return await source.ToArray();
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
return Array.Empty<T>();
} Prevention
- Return AsyncObservable.Empty<T>() from producers instead of null.
- Coalesce sources at pipeline composition points.
- Turn on nullable reference type warnings and fix upstream null flows.
When it happens
Trigger: Calling source.ToArray() where source is null: a null-returning factory, an uninitialized pipeline variable, or a conditional query builder that skipped assignment.
Common situations: Methods that return null instead of AsyncObservable.Empty<T>() on failure; async initialization ordering where the query is built before the source is created; concatenating pipeline stages where one stage returns null.
Related errors
- ArgumentNullException
- Value cannot be null. (Parameter 'source')
- source
- Value cannot be null. (Parameter 'clock')
- Value cannot be null. (Parameter 'onCompletedAsync')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/74a4a8b3754105d8.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToArray.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.Collections.Generic;
namespace System.Reactive.Linq
{
public partial class AsyncObservable
{
public static IAsyncObservable<TSource[]> ToArray<TSource>(this IAsyncObservable<TSource> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
return Create<TSource, TSource[]>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.ToArray(observer)));
}
}
public partial class AsyncObserver
{
public static IAsyncObserver<TSource> ToArray<TSource>(IAsyncObserver<TSource[]> observer)
{
if (observer == null)
throw new ArgumentNullException(nameof(observer));
return Aggregate<TSource, List<TSource>, TSource[]>(observer, new List<TSource>(), (xs, x) => { xs.Add(x); return xs; }, xs => xs.ToArray());
}
}
}
View on GitHub (pinned to 94b5d5ab91)