{"record":{"id":"609d9a770e5c011c","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-count","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'observer')","messagePattern":"Value cannot be null\\. \\(Parameter 'observer'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Count.cs","lineNumber":51,"sourceCode":"        {\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\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> Count<TSource>(IAsyncObserver<int> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            var count = 0;\n\n            return Create<TSource>(\n                async x =>\n                {\n                    try\n                    {\n                        checked\n                        {\n                            count++;\n                        }\n                    }\n                    catch (Exception ex)\n                    {\n                        await observer.OnErrorAsync(ex).ConfigureAwait(false);\n                        return;\n                    }","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Count.cs#L33-L69","documentation":"AsyncObserver.Count<TSource>(observer) throws ArgumentNullException because the downstream IAsyncObserver<int> is null. This low-level observer factory increments a counter and must deliver the final count to the given observer; with a null observer the result would be undeliverable, so it fails fast at construction.","triggerScenarios":"Calling AsyncObserver.Count<TSource>(null) inside custom operator composition, e.g. inside a Create delegate where the observer parameter was misnamed or shadowed by a null variable.","commonSituations":"Authoring custom AsyncRx.NET operators; miswiring in Create<TSource,int> callbacks; test harnesses constructing observers manually and passing null placeholders.","solutions":["Pass the observer supplied to your subscribe callback: (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Count<T>(observer)).","Create a terminal observer via AsyncObserver.Create<int>(onNextAsync, ...) to collect the count.","Prefer the high-level AsyncObservable.Count() extension, which wires observers correctly."],"exampleFix":"// before\nreturn Create<int, int>((source, observer) => source.SubscribeSafeAsync(AsyncObserver.Count<int>(null)));\n// after\nreturn Create<int, int>((source, observer) => source.SubscribeSafeAsync(AsyncObserver.Count<int>(observer)));","handlingStrategy":"try-catch","validationCode":"if (observer is null) throw new ArgumentNullException(nameof(observer));","typeGuard":"static bool HasObserver<T>(IAsyncObserver<T>? o) => o is not null;","tryCatchPattern":"try { var obs = AsyncObserver.Count<T>(observer); } catch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* wire the real downstream observer */ }","preventionTips":["Inside Create callbacks, always forward the observer parameter — never a local placeholder.","Prefer AsyncObservable.Count() over manual AsyncObserver.Count wiring.","In custom operators, capture the observer at the top of the Subscribe delegate."],"tags":["argument-null","observer","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"}