{"record":{"id":"d5efaa3e60dc03c8","repo":"dotnet/reactive","slug":"observer-observablebase","errorCode":null,"errorMessage":"observer","messagePattern":"observer","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/ObservableBase.cs","lineNumber":30,"sourceCode":"    /// <remarks>\n    /// If you don't need a named type to create an observable sequence (i.e. you rather need\n    /// an instance rather than a reusable type), use the Observable.Create method to create\n    /// an observable sequence with specified subscription behavior.\n    /// </remarks>\n    /// <typeparam name=\"T\">The type of the elements in the sequence.</typeparam>\n    public abstract class ObservableBase<T> : IObservable<T>\n    {\n        /// <summary>\n        /// Subscribes the given observer to the observable sequence.\n        /// </summary>\n        /// <param name=\"observer\">Observer that will receive notifications from the observable sequence.</param>\n        /// <returns>Disposable object representing an observer's subscription to the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"observer\"/> is <c>null</c>.</exception>\n        public IDisposable Subscribe(IObserver<T> observer)\n        {\n            if (observer == null)\n            {\n                throw new ArgumentNullException(nameof(observer));\n            }\n\n            var autoDetachObserver = new AutoDetachObserver<T>(observer);\n\n            if (CurrentThreadScheduler.IsScheduleRequired)\n            {\n                //\n                // Notice we don't protect this piece of code using an exception handler to\n                // redirect errors to the OnError channel. This call to Schedule will run the\n                // trampoline, so we'd be catching all exceptions, including those from user\n                // callbacks that happen to run there. For example, consider:\n                //\n                //    Observable.Return(42, Scheduler.CurrentThread)\n                //              .Subscribe(x => { throw new Exception(); });\n                //\n                // Here, the OnNext(42) call would be scheduled on the trampoline, so when we\n                // return from the scheduled Subscribe call, the CurrentThreadScheduler moves\n                // on to invoking this work item. Too much of protection here would cause the","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/ObservableBase.cs#L12-L48","documentation":"ObservableBase<T>.Subscribe is the base subscription entry point that wraps the observer in an AutoDetachObserver. It throws ArgumentNullException for 'observer' when a null IObserver<T> is passed. The check occurs before scheduling so the error points at the caller's subscription.","triggerScenarios":"Calling observable.Subscribe(null) where the concrete observable derives from ObservableBase<T> (ObservableBase.cs:26) — the observer argument is null.","commonSituations":"Passing a custom IObserver<T> implementation stored in a field that was never assigned; an observer created by a factory that returned null; incorrect overload selection leading to a null first argument.","solutions":["Pass a fully constructed observer instance to Subscribe","Use delegate-based Subscribe(onNext/onError/onCompleted) overloads which build the observer for you","Null-check the observer variable before subscribing"],"exampleFix":"// before\nIObserver<int> observer = _observers.TryGet(id); // may be null\nobservable.Subscribe(observer);\n// after\nvar observer = _observers.TryGet(id) ?? new AnonymousObserver<int>(x => {}, e => {}, () => {});\nobservable.Subscribe(observer);","handlingStrategy":"validation","validationCode":"if (observer == null) throw new InvalidOperationException(\"observer is not initialized\");","typeGuard":null,"tryCatchPattern":"try { observable.Subscribe(observer); } catch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* create a valid observer and resubscribe */ }","preventionTips":["Assign observer fields before subscribing","Prefer Subscribe(onNext, onError, onCompleted) so Rx builds the observer"],"tags":["argument-null","rx","null-check","csharp"],"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"}