{"record":{"id":"89477000cd15cbf1","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-takelast","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLast.cs","lineNumber":17,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Collections.Generic;\nusing System.Reactive.Concurrency;\nusing System.Reactive.Disposables;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> TakeLast<TSource>(this IAsyncObservable<TSource> source, int count)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (count < 0)\n                throw new ArgumentOutOfRangeException(nameof(count));\n\n            if (count == 0)\n            {\n                return Empty<TSource>();\n            }\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                count,\n                static async (source, count, observer) =>\n                {\n                    var (sink, drain) = AsyncObserver.TakeLast(observer, count);\n\n                    var subscription = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);\n\n                    return StableCompositeAsyncDisposable.Create(subscription, drain);","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeLast.cs#L1-L35","documentation":"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.","triggerScenarios":"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.","commonSituations":"Chaining from a factory method that returned null; optional pipeline stages that leave the observable null; LINQ-style chains on nullable results.","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."],"exampleFix":"// before: var result = maybeSource.TakeLast(5); // maybeSource is null  // after: var result = (maybeSource ?? AsyncObservable.Empty<int>()).TakeLast(5);","handlingStrategy":"validation","validationCode":"if (source == null) source = AsyncObservable.Empty<TSource>(); var last = source.TakeLast(count);","typeGuard":"static IAsyncObservable<TSource> OrEmpty<TSource>(IAsyncObservable<TSource> s) => s ?? AsyncObservable.Empty<TSource>();","tryCatchPattern":"try { var last = source.TakeLast(count); } catch (ArgumentNullException ex) when (ex.ParamName == \"source\") { var last = AsyncObservable.Empty<TSource>(); }","preventionTips":["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."],"tags":["csharp","null-argument","observable"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}