{"record":{"id":"76f9be21a2b6fb51","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-onnextasync-76f9be","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'onNextAsync')","messagePattern":"Value cannot be null\\. \\(Parameter 'onNextAsync'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Internal/UnsafeAsyncObserver.cs","lineNumber":17,"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 UnsafeAsyncObserver<T> : IAsyncObserver<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 UnsafeAsyncObserver(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        public ValueTask OnCompletedAsync() => _onCompletedAsync();\n\n        public ValueTask OnErrorAsync(Exception error) => _onErrorAsync(error ?? throw new ArgumentNullException(nameof(error)));\n\n        public ValueTask OnNextAsync(T value) => _onNextAsync(value);\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Internal/UnsafeAsyncObserver.cs#L1-L29","documentation":"UnsafeAsyncObserver wraps three delegates (onNextAsync, onErrorAsync, onCompletedAsync) that its On*Async methods invoke directly; null delegates would crash mid-stream. The constructor therefore throws ArgumentNullException for the first, onNextAsync, when null.","triggerScenarios":"Calling new UnsafeAsyncObserver<T>(null, onErrorAsync, onCompletedAsync) — e.g. building an observer programmatically where the next-handler is conditionally constructed or comes from a null-returning factory.","commonSituations":"Constructing observers from configuration/delegates loaded from DI where one binding is missing; generic helper methods forwarding possibly-null lambdas; typos passing arguments in the wrong order.","solutions":["Pass a non-null Func<T, ValueTask> as onNextAsync (use _ => default(ValueTask) for a no-op).","Check ordering of arguments — onNext comes first, before onErrorAsync and onCompletedAsync.","Fix the producer/factory returning the null delegate."],"exampleFix":"// before\nvar observer = new UnsafeAsyncObserver<int>(maybeNext, OnError, OnCompleted);\n// after\nvar observer = new UnsafeAsyncObserver<int>(\n    maybeNext ?? (x => default), OnError, OnCompleted);","handlingStrategy":"validation","validationCode":"if (onNextAsync is null) throw new ArgumentNullException(nameof(onNextAsync));\nvar observer = new UnsafeAsyncObserver<T>(onNextAsync, onErrorAsync, onCompletedAsync);","typeGuard":"static bool HasOnNext<T>(Func<T, ValueTask>? f) => f is not null;","tryCatchPattern":"try { var obs = new UnsafeAsyncObserver<int>(onNext, onError, onCompleted); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"onNextAsync\")\n{ log.LogError(\"onNextAsync delegate was null\"); }","preventionTips":["Use no-op lambdas (_ => default) instead of null for unused observer callbacks.","Double-check argument order: onNextAsync comes first.","Load delegate dependencies from DI with required bindings so they cannot be null."],"tags":["argument-null","observer","async-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-15T23:17:13.987Z"}