dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'source')
Error message
Value cannot be null. (Parameter 'source')
What it means
TakeLast(source, count) requires a non-null source observable. The guard at TakeLast.cs:17 throws ArgumentNullException because there is nothing to subscribe to; the library validates parameters eagerly rather than throwing at subscribe time.
Solutions
- Ensure the source is created before chaining (e.g. AsyncObservable.Empty<T>() as fallback).
- Find and fix the factory/branch producing a null source.
- Use the null-conditional pattern or an explicit check to substitute an empty observable when absent.
- Avoid storing observables in nullable fields without initializing them.
Example fix
// before: var result = maybeSource.TakeLast(5); // maybeSource is null // after: var result = (maybeSource ?? AsyncObservable.Empty<int>()).TakeLast(5);
Defensive patterns
Strategy: validation
Validate before calling
if (source == null) source = AsyncObservable.Empty<TSource>(); var last = source.TakeLast(count);
Type guard
static IAsyncObservable<TSource> OrEmpty<TSource>(IAsyncObservable<TSource> s) => s ?? AsyncObservable.Empty<TSource>();
Try / catch
try { var last = source.TakeLast(count); } catch (ArgumentNullException ex) when (ex.ParamName == "source") { var last = AsyncObservable.Empty<TSource>(); } Prevention
- Never leave observable fields uninitialized; default to Empty<T>().
- Null-check factory return values before chaining.
- Prefer throwing over returning null from methods that produce observables.
- Use the ??-or-empty pattern at pipeline entry points.
When it happens
Trigger: Calling source.TakeLast(n) where source is a null IAsyncObservable — typically a method/property returning null, or a variable never assigned because a conditional branch was skipped.
Common situations: Chaining from a factory method that returned null; optional pipeline stages that leave the observable null; LINQ-style chains on nullable results.
Related errors
- source
- Value cannot be null. (Parameter 'onCompletedAsync')
- Value cannot be null. (Parameter 'onNext')
- Value cannot be null. (Parameter 'onError')
- Value cannot be null. (Parameter 'observer')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/89477000cd15cbf1.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLast.cs:17
// 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;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Threading.Tasks;
namespace System.Reactive.Linq
{
public partial class AsyncObservable
{
public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, int count)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count));
if (count == 0)
{
return Empty<TSource>();
}
return CreateAsyncObservable<TSource>.From(
source,
count,
static async (source, count, observer) =>
{
var (sink, drain) = AsyncObserver.TakeLast(observer, count);
var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
return StableCompositeAsyncDisposable.Create(subscription, drain);View on GitHub (pinned to 94b5d5ab91)