{"record":{"id":"cf14b93f96a40ffd","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-count","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/Count.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<int> Count<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create<TSource, int>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Count<TSource>(observer)));\n        }\n\n        public static IAsyncObservable<int> Count<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<int>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Count(observer, predicate)));\n        }\n\n        public static IAsyncObservable<int> Count<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/Count.cs#L1-L32","documentation":"The parameterless Count extension throws ArgumentNullException because the IAsyncObservable<TSource> source is null. AsyncRx.NET extensions validate the source before subscribing so you get the error at the Count() call site rather than deep inside the async subscription. Counting requires a source stream to iterate.","triggerScenarios":"Calling source.Count() where source came from a failed factory, an unassigned field, or a method that returned null.","commonSituations":"Chaining operators after a method that conditionally returns null; a cached observable that was never initialized; refactoring where the source creation line was removed.","solutions":["Ensure the observable is created before counting, e.g. via AsyncObservable.Create or another operator.","Null-check or coalesce the source before calling Count (e.g. source ?? AsyncObservable.Empty<T>()).","Trace the factory method that produced the source and fix its null return path."],"exampleFix":"// before\nvar count = await possiblyNullSource.Count();\n// after\nif (possiblyNullSource == null) throw new InvalidOperationException(\"source not initialized\");\nvar count = await possiblyNullSource.Count();","handlingStrategy":"validation","validationCode":"if (source is null) throw new InvalidOperationException(\"Source observable was not initialized before Count().\");","typeGuard":"static bool HasSource<T>(IAsyncObservable<T>? s) => s is not null;","tryCatchPattern":"try { var n = await source.Count(); } catch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* fix source initialization */ }","preventionTips":["Treat null observables as bugs; have factories throw rather than return null.","Use nullable reference types to catch uninitialized source fields at compile time.","Coalesce to AsyncObservable.Empty<T>() only when null legitimately means 'no data'."],"tags":["argument-null","async-rx","extension-method"],"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"}