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

  1. Ensure the source is created before chaining (e.g. AsyncObservable.Empty<T>() as fallback).
  2. Find and fix the factory/branch producing a null source.
  3. Use the null-conditional pattern or an explicit check to substitute an empty observable when absent.
  4. 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

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


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)