{"record":{"id":"bc783197a9b52e4c","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-asasyncobservable","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/AsAsyncObservable.cs","lineNumber":12,"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\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> AsAsyncObservable<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create(source, static (source, observer) => source.SubscribeSafeAsync(observer));\n        }\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":18,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/AsAsyncObservable.cs#L1-L18","documentation":"AsAsyncObservable wraps an IAsyncObservable<TSource> to guarantee a stable async-observable identity, and it throws ArgumentNullException when the source is null. The library validates arguments eagerly at the call site so subscription-time failures are avoided. There is no meaningful wrapper for a null source.","triggerScenarios":"Calling source.AsAsyncObservable() where the extension-method receiver is null — e.g. a method returning IAsyncObservable<T> returned null, a dictionary/lookup miss yielded null, or an optional dependency was not configured.","commonSituations":"Chaining AsAsyncObservable onto the result of a factory that returned null; calling it on a field populated asynchronously that has not been initialized yet; interop code where a null sync observable was converted to a null async one.","solutions":["Ensure the receiver is a real observable before calling, e.g. source ?? AsyncObservable.Empty<T>() or another default instance.","Fix the upstream factory/lookup that produced the null source.","Add a null check with a clear exception at the point where the source is created, so the failure is reported with context.","If the value is legitimately optional, branch on null instead of calling the operator unconditionally."],"exampleFix":"// before\nvar wrapped = maybeSource.AsAsyncObservable(); // maybeSource is null\n\n// after\nif (maybeSource == null) throw new InvalidOperationException(\"source was not initialized\");\nvar wrapped = maybeSource.AsAsyncObservable();","handlingStrategy":"validation","validationCode":"if (source == null)\n    source = AsyncObservable.Empty<T>();","typeGuard":"static bool IsObservable<T>(IAsyncObservable<T>? o) => o is not null;","tryCatchPattern":"try\n{\n    var wrapped = maybeSource.AsAsyncObservable();\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    var wrapped = AsyncObservable.Empty<T>();\n}","preventionTips":["Make producers return AsyncObservable.Empty<T>() instead of null","Null-check factory results before chaining operators","Keep nullable observables out of APIs; model 'no data' as an empty stream"],"tags":["dotnet","asyncrx","argument-null","null-source"],"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"}