{"record":{"id":"9f91fcb4256f9f67","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-onerror-anonymousobserver","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'onError')","messagePattern":"Value cannot be null\\. \\(Parameter 'onError'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/AnonymousObserver.cs","lineNumber":27,"sourceCode":"    /// </summary>\n    /// <typeparam name=\"T\">The type of the elements in the sequence.</typeparam>\n    public sealed class AnonymousObserver<T> : ObserverBase<T>\n    {\n        private readonly Action<T> _onNext;\n        private readonly Action<Exception> _onError;\n        private readonly Action _onCompleted;\n\n        /// <summary>\n        /// Creates an observer from the specified <see cref=\"IObserver{T}.OnNext(T)\"/>, <see cref=\"IObserver{T}.OnError(Exception)\"/>, and <see cref=\"IObserver{T}.OnCompleted()\"/> actions.\n        /// </summary>\n        /// <param name=\"onNext\">Observer's <see cref=\"IObserver{T}.OnNext(T)\"/> action implementation.</param>\n        /// <param name=\"onError\">Observer's <see cref=\"IObserver{T}.OnError(Exception)\"/> action implementation.</param>\n        /// <param name=\"onCompleted\">Observer's <see cref=\"IObserver{T}.OnCompleted()\"/> action implementation.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"onNext\"/> or <paramref name=\"onError\"/> or <paramref name=\"onCompleted\"/> is <c>null</c>.</exception>\n        public AnonymousObserver(Action<T> onNext, Action<Exception> onError, Action onCompleted)\n        {\n            _onNext = onNext ?? throw new ArgumentNullException(nameof(onNext));\n            _onError = onError ?? throw new ArgumentNullException(nameof(onError));\n            _onCompleted = onCompleted ?? throw new ArgumentNullException(nameof(onCompleted));\n        }\n\n        /// <summary>\n        /// Creates an observer from the specified <see cref=\"IObserver{T}.OnNext(T)\"/> action.\n        /// </summary>\n        /// <param name=\"onNext\">Observer's <see cref=\"IObserver{T}.OnNext(T)\"/> action implementation.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"onNext\"/> is <c>null</c>.</exception>\n        public AnonymousObserver(Action<T> onNext)\n            : this(onNext, Stubs.Throw, Stubs.Nop)\n        {\n        }\n\n        /// <summary>\n        /// Creates an observer from the specified <see cref=\"IObserver{T}.OnNext(T)\"/> and <see cref=\"IObserver{T}.OnError(Exception)\"/> actions.\n        /// </summary>\n        /// <param name=\"onNext\">Observer's <see cref=\"IObserver{T}.OnNext(T)\"/> action implementation.</param>\n        /// <param name=\"onError\">Observer's <see cref=\"IObserver{T}.OnError(Exception)\"/> action implementation.</param>","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/AnonymousObserver.cs#L9-L45","documentation":"The AnonymousObserver constructor throws ArgumentNullException when the onError action is null. Rx requires an explicit error handler so exceptions from the sequence are observed rather than silently swallowed; passing null onError is rejected at construction.","triggerScenarios":"Calling new AnonymousObserver<T>(onNext, null, onCompleted) or source.Subscribe(onNext, onError: null, onCompleted), e.g. source.Subscribe(x => ..., null, () => ...) where a logging/error delegate variable is null.","commonSituations":"Logging integrations where the error logger is not configured; test scaffolding that only wires onNext and onCompleted; plugin systems where the error handler is optional but the API demands it.","solutions":["Supply a real onError action, e.g. ex => Console.Error.WriteLine(ex)","If errors can be ignored deliberately, pass a no-op: _ => { }","Configure/default the error handler before subscribing"],"exampleFix":"// before\nvar sub = source.Subscribe(x => Handle(x), null, () => Done());\n// after\nvar sub = source.Subscribe(x => Handle(x), ex => Log(ex), () => Done());","handlingStrategy":"type-guard","validationCode":"// C#\nAction<Exception>? onError = ResolveErrorHandler();\nif (onError is null)\n    onError = ex => Console.Error.WriteLine(ex);\nvar sub = source.Subscribe(x => Handle(x), onError, () => Done());","typeGuard":"bool HasErrorHandler(Action<Exception>? a) => a is not null;","tryCatchPattern":"try { var sub = source.Subscribe(onNext, onError, onCompleted); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"onError\")\n{\n    // supply a default error logger\n}","preventionTips":["Always pass an error handler — Rx sequences commonly fail","Default missing loggers to Console.Error.WriteLine","Avoid passing null positionally in the 3-arg Subscribe overload"],"tags":["rx","observer","subscribe","null-argument"],"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"}