{"record":{"id":"322ca40c569cbe67","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-toarray","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'observer')","messagePattern":"Value cannot be null\\. \\(Parameter 'observer'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToArray.cs","lineNumber":25,"sourceCode":"namespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource[]> ToArray<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create<TSource, TSource[]>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.ToArray(observer)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> ToArray<TSource>(IAsyncObserver<TSource[]> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            return Aggregate<TSource, List<TSource>, TSource[]>(observer, new List<TSource>(), (xs, x) => { xs.Add(x); return xs; }, xs => xs.ToArray());\n        }\n    }\n}\n","sourceCodeStart":7,"sourceCodeEnd":31,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToArray.cs#L7-L31","documentation":"AsyncObserver.ToArray throws ArgumentNullException because the downstream IAsyncObserver<TSource[]> passed to it is null. This library eagerly validates every public operator argument with ArgumentNullException guards, so calling ToArray with a null observer fails immediately instead of surfacing later inside the aggregation pipeline. It is a programming/usage error, not a runtime data condition.","triggerScenarios":"Calling AsyncObserver.ToArray<TSource>(null); passing a field or return value that is null because an earlier subscribe/pipe step produced no observer.","commonSituations":"Refactoring pipelines where an observer variable is not yet assigned; conditional observer construction that returned null on some path; DI containers that failed to resolve the observer dependency.","solutions":["Ensure the observer argument is a non-null IAsyncObserver<TSource[]> before calling ToArray.","Check the code that creates the observer (e.g. CreateObserver or another operator's output) for a path that yields null.","Add an assert/guard at the call site to fail fast with a meaningful message."],"exampleFix":"// before\nIAsyncObserver<int[]> observer = BuildObserver(); // may return null\nvar a = AsyncObserver.ToArray<int>(observer);\n// after\nvar obs = BuildObserver() ?? throw new InvalidOperationException(\"observer not built\");\nvar a = AsyncObserver.ToArray<int>(obs);","handlingStrategy":"validation","validationCode":"if (observer is null) throw new InvalidOperationException(\"observer must be constructed before ToArray\");\nvar arr = AsyncObserver.ToArray<TSource>(observer);","typeGuard":"static bool IsValidObserver<T>(IAsyncObserver<T> o) => o is not null;","tryCatchPattern":"try { var a = AsyncObserver.ToArray<int>(observer); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* supply valid observer */ }","preventionTips":["Construct observers immediately before use, never lazily via unassigned fields","Never return null from observer factory methods; throw instead","Enable nullable reference types (C# 8+) to catch nulls at compile time"],"tags":["csharp","async-rx","argument-null","operator"],"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"}