{"record":{"id":"74a4a8b3754105d8","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-toarray","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToArray.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.Collections.Generic;\n\nnamespace 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":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ToArray.cs#L1-L31","documentation":"The ToArray operator throws ArgumentNullException with message \"Value cannot be null. (Parameter 'source')\" when the source IAsyncObservable<TSource> is null. ToArray buffers all elements into a TSource[] emitted on completion, and buffering requires a valid source, validated at the operator call.","triggerScenarios":"Calling source.ToArray() where source is null: a null-returning factory, an uninitialized pipeline variable, or a conditional query builder that skipped assignment.","commonSituations":"Methods that return null instead of AsyncObservable.Empty<T>() on failure; async initialization ordering where the query is built before the source is created; concatenating pipeline stages where one stage returns null.","solutions":["Ensure the source is non-null; substitute AsyncObservable.Empty<TSource>() for an intentionally empty stream.","ThrowIfNull the source where the observable is produced so the failure points to the real defect.","Fix the producing method/branch to always return a valid observable."],"exampleFix":"// before\nvar items = GetStream(); // null on failure\nvar arr = await items.ToArray();\n\n// after\nvar items = GetStream() ?? AsyncObservable.Empty<int>();\nvar arr = await items.ToArray();","handlingStrategy":"validation","validationCode":"if (source is null)\n    throw new InvalidOperationException(\"ToArray requires a non-null source observable.\");","typeGuard":"bool HasSource<T>(IAsyncObservable<T>? source) => source is not null;","tryCatchPattern":"try\n{\n    return await source.ToArray();\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    return Array.Empty<T>();\n}","preventionTips":["Return AsyncObservable.Empty<T>() from producers instead of null.","Coalesce sources at pipeline composition points.","Turn on nullable reference type warnings and fix upstream null flows."],"tags":["csharp","asyncrx","argument-validation","null-check","operators"],"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"}