{"record":{"id":"5f19216245c23962","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observable","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'observable')","messagePattern":"Value cannot be null\\. \\(Parameter 'observable'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Threading/Tasks/TaskObservableExtensions.cs","lineNumber":591,"sourceCode":"            }\n        }\n\n        /// <summary>\n        /// Returns a task that will receive the last value or the exception produced by the observable sequence.\n        /// </summary>\n        /// <typeparam name=\"TResult\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"observable\">Observable sequence to convert to a task.</param>\n        /// <param name=\"cancellationToken\">Cancellation token that can be used to cancel the task, causing unsubscription from the observable sequence.</param>\n        /// <param name=\"state\">The state to use as the underlying task's <see cref=\"Task.AsyncState\"/>.</param>\n        /// <returns>A task that will receive the last element or the exception produced by the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"observable\"/> is <c>null</c>.</exception>\n#pragma warning disable CA1068 // (CancellationToken parameters must come last.) Would be a breaking change.\n        public static Task<TResult> ToTask<TResult>(this IObservable<TResult> observable, CancellationToken cancellationToken, object? state)\n#pragma warning restore CA1068\n        {\n            if (observable == null)\n            {\n                throw new ArgumentNullException(nameof(observable));\n            }\n\n            var tcs = new TaskCompletionSource<TResult>(state);\n\n            var taskCompletionObserver = new ToTaskObserver<TResult>(tcs, cancellationToken);\n\n            //\n            // Subtle race condition: if the source completes before we reach the line below, the SingleAssigmentDisposable\n            // will already have been disposed. Upon assignment, the disposable resource being set will be disposed on the\n            // spot, which may throw an exception.\n            //\n            try\n            {\n                //\n                // [OK] Use of unsafe Subscribe: we're catching the exception here to set the TaskCompletionSource.\n                //\n                // Notice we could use a safe subscription to route errors through OnError, but we still need the\n                // exception handling logic here for the reason explained above. We cannot afford to throw here","sourceCodeStart":573,"sourceCodeEnd":609,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Threading/Tasks/TaskObservableExtensions.cs#L573-L609","documentation":"ToTask<TResult> throws ArgumentNullException with parameter name 'observable' when the source IObservable is null. The extension method validates its receiver before creating the TaskCompletionSource and observer.","triggerScenarios":"Invoking source.ToTask() where the source reference is null (extension methods can be called on null receivers).","commonSituations":"Nullable fields/properties holding the observable that were never initialized; factory methods returning null; chaining pipelines where an earlier operator produced null.","solutions":["Ensure the observable is initialized before calling ToTask.","Null-check or use the null-conditional operator (?.) with a fallback.","Fix upstream factory/DI code that returns null instead of an empty observable (Observable.Empty<TResult>())."],"exampleFix":"// before\nvar task = maybeObservable.ToTask();\n// after\nvar task = (maybeObservable ?? Observable.Empty<TResult>()).ToTask();","handlingStrategy":"type-guard","validationCode":"if (observable == null) return Task.FromResult(default(TResult)!);","typeGuard":"bool IsValidSource<TResult>(IObservable<TResult>? o) => o is not null;\n// usage: if (IsValidSource(source)) { var t = source.ToTask(); }","tryCatchPattern":"try { task = source.ToTask(); }\ncatch (ArgumentNullException) { task = Task.FromCanceled<TResult>(CancellationToken.None); }","preventionTips":["Initialize observable fields with Observable.Empty<T>() instead of null.","Ensure factories/DI never return null observables."],"tags":["argument-null","totask","rx"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}