{"record":{"id":"f36b080f361e494c","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-longcount","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/LongCount.cs","lineNumber":14,"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.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<long> LongCount<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create<TSource, long>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.LongCount<TSource>(observer)));\n        }\n\n        public static IAsyncObservable<long> LongCount<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return CreateAsyncObservable<long>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.LongCount(observer, predicate)));\n        }\n\n        public static IAsyncObservable<long> LongCount<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/LongCount.cs#L1-L32","documentation":"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.","triggerScenarios":"Calling AsyncObservable.LongCount(null) with or without a predicate — typically when the source expression (a factory, cache lookup, or field) evaluated to null.","commonSituations":"Passing a nullable observable field that was never initialized, a method returning null instead of an empty observable, or refactoring that broke source assignment.","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"],"exampleFix":"// before\nvar count = await source.LongCountAsync();\n// after\nvar count = await (source ?? AsyncObservable.Empty<int>()).LongCountAsync();","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\n// or semantically-correct fallback:\nvar safeSource = source ?? AsyncObservable.Empty<TSource>();","typeGuard":"static bool IsObservableSet<T>(IAsyncObservable<T>? s) => s is not null;","tryCatchPattern":"try\n{\n    var count = await source.LongCountAsync();\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    // source missing; treat count as 0 or surface config error\n}","preventionTips":["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"],"tags":["argumentnull","null-source","linq-operator","async-rx"],"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"}