{"record":{"id":"47607b6c61abb569","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-take","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/Take.cs","lineNumber":94,"sourceCode":"                source,\n                (duration, scheduler),\n                static async (source, state, observer) =>\n                {\n                    var (sourceObserver, timer) = await AsyncObserver.Take(observer, state.duration).ConfigureAwait(false);\n\n                    var subscription = await source.SubscribeSafeAsync(sourceObserver).ConfigureAwait(false);\n\n                    return StableCompositeAsyncDisposable.Create(subscription, timer);\n                });\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> Take<TSource>(IAsyncObserver<TSource> observer, int count)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (count <= 0)\n                throw new ArgumentOutOfRangeException(nameof(count));\n\n            return Create<TSource>(\n                async x =>\n                {\n                    var remaining = --count;\n\n                    await observer.OnNextAsync(x).ConfigureAwait(false);\n\n                    if (remaining == 0)\n                    {\n                        await observer.OnCompletedAsync().ConfigureAwait(false);\n                    }\n                },\n                observer.OnErrorAsync,\n                observer.OnCompletedAsync\n            );","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Take.cs#L76-L112","documentation":"The observer-based Take(observer, count) factory requires a non-null IAsyncObserver. Passing null makes the resulting operator unable to forward notifications, so the guard at Take.cs:94 throws ArgumentNullException immediately.","triggerScenarios":"Calling AsyncObserver.Take(null, count); passing the result of a failed composition chain (a method that returned null) into Take.","commonSituations":"Building custom async observer pipelines manually; intermediate factory returning null after a refactor; misordered arguments so that null lands in the observer slot.","solutions":["Pass a valid IAsyncObserver instance (e.g. one created via AsyncObserver.Create).","Check the expression producing the observer; fix whatever returns null upstream.","Verify argument order — count and observer are easily swapped in handwritten calls.","Null-check or assert the observer before calling Take in custom pipeline builders."],"exampleFix":"// before: AsyncObserver.Take(null, 3)  // after: var inner = AsyncObserver.Create<int>(...); AsyncObserver.Take(inner, 3)","handlingStrategy":"validation","validationCode":"if (observer == null) throw new ArgumentNullException(nameof(observer)); if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count)); var obs = AsyncObserver.Take(observer, count);","typeGuard":"static bool CanTake<TSource>(IAsyncObserver<TSource> obs, int count) => obs != null && count > 0;","tryCatchPattern":"try { var obs = AsyncObserver.Take(observer, count); } catch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* observer chain was not initialized; abort or substitute */ }","preventionTips":["Null-check observers before composing operator pipelines.","Assert argument order (observer first, count second).","Avoid factories that can return null IAsyncObserver; throw early instead.","Unit-test pipeline builders with null inputs."],"tags":["csharp","null-argument","observer"],"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"}