{"record":{"id":"327026da3228b994","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-single","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/Single.cs","lineNumber":55,"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<TSource>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Single(observer, predicate)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> Single<TSource>(IAsyncObserver<TSource> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            var hasValue = false;\n            var value = default(TSource);\n\n            return Create<TSource>(\n                async x =>\n                {\n                    if (hasValue)\n                    {\n                        await observer.OnErrorAsync(new InvalidOperationException(\"The sequence contains more than one element.\")).ConfigureAwait(false);\n                        return;\n                    }\n\n                    hasValue = true;\n                    value = x;\n                },\n                observer.OnErrorAsync,\n                async () =>","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Single.cs#L37-L73","documentation":"Thrown by AsyncObserver.Single<TSource>(IAsyncObserver<TSource>) when the downstream observer is null. This is the observer-side factory used internally by the operator but it is public, so direct callers must supply a valid observer. The guard exists to keep the async observer chain well-formed from the start.","triggerScenarios":"Calling AsyncObserver.Single(observer) directly with a null observer, typically when composing custom async observer pipelines.","commonSituations":"Custom operator implementations passing through an uninitialized observer field; test harnesses constructing observers manually.","solutions":["Pass a valid IAsyncObserver<TSource> instance","In custom operators, ensure the observer parameter is validated or propagated from the subscription call","Use Create(source, ...) operators instead of composing raw AsyncObserver factories"],"exampleFix":"// before\nvar obs = AsyncObserver.Single<int>(null);\n// after\nvar obs = AsyncObserver.Single<int>(myDownstreamObserver);","handlingStrategy":"validation","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 o = AsyncObserver.Single<T>(observer); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* construct or substitute observer */ }","preventionTips":["Obtain observers from SubscribeAsync callbacks, never store them in nullable fields","Validate observer parameters at the top of custom operators","Favor operator-level APIs over raw AsyncObserver composition"],"tags":["csharp","null-check","observer","asyncrx"],"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"}