{"record":{"id":"c751bc81c840b028","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-onnext-anonymousobserver","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'onNext')","messagePattern":"Value cannot be null\\. \\(Parameter 'onNext'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/AnonymousObserver.cs","lineNumber":26,"sourceCode":"    /// Class to create an <see cref=\"IObserver{T}\"/> instance from delegate-based implementations of the On* methods.\n    /// </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>","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/AnonymousObserver.cs#L8-L44","documentation":"The AnonymousObserver constructor throws ArgumentNullException when the onNext action is null. AnonymousObserver powers Observable.Subscribe(onNext, onError, onCompleted); all three callbacks are invoked as the source emits, and the library requires explicit non-null handlers rather than silently ignoring notifications.","triggerScenarios":"Calling new AnonymousObserver<T>(null, onError, onCompleted) or source.Subscribe(onNext: null, onError, onCompleted) / Subscribe((Action<T>)null, ex => ..., () => ...) with a null onNext while supplying the other two handlers.","commonSituations":"UI/event handlers where the onNext callback comes from an unset delegate or event field; dynamic subscription builders from config where the next-handler key is missing; refactors that removed the body but kept the null variable.","solutions":["Supply a real onNext action, e.g. x => Console.WriteLine(x)","If onNext is genuinely unneeded, pass a no-op: _ => { }","Null-check or default the callback before subscribing"],"exampleFix":"// before\nvar sub = source.Subscribe(nextHandler, ex => Log(ex), () => Done()); // nextHandler null\n// after\nvar sub = source.Subscribe(nextHandler ?? (_ => { }), ex => Log(ex), () => Done());","handlingStrategy":"type-guard","validationCode":"// C#\nAction<int>? onNext = ResolveNext();\nif (onNext is null)\n    onNext = _ => { };\nvar sub = source.Subscribe(onNext, ex => Log(ex), () => Done());","typeGuard":"bool HasNext<T>(Action<T>? a) => a is not null;","tryCatchPattern":"try { var sub = source.Subscribe(onNext, onError, onCompleted); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"onNext\")\n{\n    // supply a default onNext or abort subscription\n}","preventionTips":["Use the overloads that take fewer handlers (Subscribe(Action<T>)) instead of passing null","Initialize callback fields with no-op delegates","Null-check callbacks sourced from config/DI before subscribing"],"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"}