{"record":{"id":"da4b96b6adfa945b","repo":"dotnet/reactive","slug":"onnext-observable-extensions","errorCode":null,"errorMessage":"onNext","messagePattern":"onNext","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs","lineNumber":57,"sourceCode":"\n        /// <summary>\n        /// Subscribes an element handler to an observable sequence.\n        /// </summary>\n        /// <typeparam name=\"T\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Observable sequence to subscribe to.</param>\n        /// <param name=\"onNext\">Action to invoke for each element in the observable sequence.</param>\n        /// <returns><see cref=\"IDisposable\"/> object used to unsubscribe from the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"onNext\"/> is <c>null</c>.</exception>\n        public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (onNext == null)\n            {\n                throw new ArgumentNullException(nameof(onNext));\n            }\n\n            //\n            // [OK] Use of unsafe Subscribe: non-pretentious constructor for an observer; this overload is not to be used internally.\n            //\n            return source.Subscribe/*Unsafe*/(new AnonymousObserver<T>(onNext, Stubs.Throw, Stubs.Nop));\n        }\n\n        /// <summary>\n        /// Subscribes an element handler and an exception handler to an observable sequence.\n        /// </summary>\n        /// <typeparam name=\"T\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Observable sequence to subscribe to.</param>\n        /// <param name=\"onNext\">Action to invoke for each element in the observable sequence.</param>\n        /// <param name=\"onError\">Action to invoke upon exceptional termination of the observable sequence.</param>\n        /// <returns><see cref=\"IDisposable\"/> object used to unsubscribe from the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"onNext\"/> or <paramref name=\"onError\"/> is <c>null</c>.</exception>\n        public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action<Exception> onError)","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs#L39-L75","documentation":"Subscribe<T>(this IObservable<T>, Action<T>) throws ArgumentNullException when the onNext handler is null. The onNext delegate is required to construct the AnonymousObserver (paired with Stubs.Throw for errors and Stubs.Nop for completion). Thrown synchronously after the source check.","triggerScenarios":"Calling source.Subscribe(null) or source.Subscribe(someActionVariable) where the Action<T> variable is null — e.g. a handler assigned conditionally or read from config/DI that wasn't set.","commonSituations":"Event-handler fields that are null until wired; passing a method group that failed to resolve to an overload with a nullable local; factory methods returning null callbacks.","solutions":["Pass a non-null onNext lambda, e.g. x => Handle(x).","If no handling is needed, use the parameterless source.Subscribe() overload.","Null-check the handler and fall back to Stubs-style ignore or skip the subscription."],"exampleFix":"// before\nsource.Subscribe(handler); // handler is null\n// after\nsource.Subscribe(handler ?? (_ => { }));","handlingStrategy":"validation","validationCode":"if (onNext == null) return System.Reactive.Disposables.Disposable.Empty;\nsource.Subscribe(onNext);","typeGuard":"bool HasHandler<T>(Action<T> onNext) => onNext != null;","tryCatchPattern":"try { source.Subscribe(onNext); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"onNext\") { source.Subscribe(_ => { }); }","preventionTips":["Use the parameterless Subscribe() when no per-element handling is needed.","Default optional handler fields to a no-op lambda instead of null.","Ensure handler lambdas assigned from config/DI are wired before subscription."],"tags":["argument-null","subscribe","reactive-extensions"],"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"}