{"record":{"id":"f036d3a860db2058","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-onerrorasync-asyncobserver","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'onErrorAsync')","messagePattern":"Value cannot be null\\. \\(Parameter 'onErrorAsync'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/AsyncObserver.cs","lineNumber":18,"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.Threading.Tasks;\n\nnamespace System.Reactive\n{\n    public class AsyncObserver<T> : AsyncObserverBase<T>\n    {\n        private readonly Func<T, ValueTask> _onNextAsync;\n        private readonly Func<Exception, ValueTask> _onErrorAsync;\n        private readonly Func<ValueTask> _onCompletedAsync;\n\n        public AsyncObserver(Func<T, ValueTask> onNextAsync, Func<Exception, ValueTask> onErrorAsync, Func<ValueTask> onCompletedAsync)\n        {\n            _onNextAsync = onNextAsync ?? throw new ArgumentNullException(nameof(onNextAsync));\n            _onErrorAsync = onErrorAsync ?? throw new ArgumentNullException(nameof(onErrorAsync));\n            _onCompletedAsync = onCompletedAsync ?? throw new ArgumentNullException(nameof(onCompletedAsync));\n        }\n\n        protected override ValueTask OnCompletedAsyncCore() => _onCompletedAsync();\n\n        protected override ValueTask OnErrorAsyncCore(Exception error) => _onErrorAsync(error ?? throw new ArgumentNullException(nameof(error)));\n\n        protected override ValueTask OnNextAsyncCore(T value) => _onNextAsync(value);\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/AsyncObserver.cs#L1-L29","documentation":"The AsyncObserver constructor null-checks all three delegates and throws ArgumentNullException naming the first null one; here onErrorAsync was null. Without an error handler the observer could not propagate faults from the async sequence, so construction is rejected eagerly (line 18).","triggerScenarios":"new AsyncObserver<T>(onNextAsync, null, onCompletedAsync) — constructing with a null error-handler delegate.","commonSituations":"Hand-building observers in tests where error handling was deemed unnecessary; wrapper libraries forwarding an optional callback straight through as null; code migrated from sync Rx where null handlers were tolerated by some implementations.","solutions":["Provide a non-null error handler, e.g. async ex => await LogErrorAsync(ex) or ex => { ...; return default; }.","Use AsyncObserver.Create or SubscribeAsync helpers that let you compose observers without constructing AsyncObserver directly.","If errors should be swallowed, pass ex => default (a no-op) rather than null.","Note the same constructor also guards OnErrorAsyncCore's error parameter — never invoke the observer with a null Exception."],"exampleFix":"// before\nvar observer = new AsyncObserver<int>(x => default, null, () => default);\n// after\nvar observer = new AsyncObserver<int>(x => default, ex => { Console.Error.WriteLine(ex); return default; }, () => default);","handlingStrategy":"validation","validationCode":"if (onNextAsync == null || onErrorAsync == null || onCompletedAsync == null)\n    throw new ArgumentException(\"AsyncObserver requires non-null onNextAsync, onErrorAsync, and onCompletedAsync delegates.\");\nvar observer = new AsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync);","typeGuard":null,"tryCatchPattern":"try\n{\n    var observer = new AsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"onErrorAsync\")\n{\n    observer = new AsyncObserver<T>(onNextAsync, ex2 => default, onCompletedAsync);\n}","preventionTips":["Always supply an error handler; swallowing via ex => default is safer than null.","Avoid forwarding optional callbacks as null — default them at the boundary.","Also never pass a null Exception to OnErrorAsyncCore; the method null-checks it too."],"tags":["argument-null","constructor","rx","async"],"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"}