{"record":{"id":"16e038aed4a12363","repo":"dotnet/reactive","slug":"observer-observable-extensions","errorCode":null,"errorMessage":"observer","messagePattern":"observer","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs","lineNumber":189,"sourceCode":"\n        /// <summary>\n        /// Subscribes an observer to an observable sequence, using a <see cref=\"CancellationToken\"/> to support unsubscription.\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=\"observer\">Observer to subscribe to the sequence.</param>\n        /// <param name=\"token\">CancellationToken that can be signaled to unsubscribe from the source sequence.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"observer\"/> is <c>null</c>.</exception>\n        public static void Subscribe<T>(this IObservable<T> source, IObserver<T> observer, CancellationToken token)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (observer == null)\n            {\n                throw new ArgumentNullException(nameof(observer));\n            }\n\n            source.Subscribe_(observer, token);\n        }\n\n        /// <summary>\n        /// Subscribes to the observable sequence without specifying any handlers, using a <see cref=\"CancellationToken\"/> to support unsubscription.\n        /// This method can be used to evaluate the observable sequence for its side-effects only.\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=\"token\">CancellationToken that can be signaled to unsubscribe from the source sequence.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is <c>null</c>.</exception>\n        public static void Subscribe<T>(this IObservable<T> source, CancellationToken token)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs#L171-L207","documentation":"The Subscribe(source, observer, token) extension throws ArgumentNullException when the IObserver<T> argument is null. Rx validates the observer upfront because a null observer would make it impossible to deliver OnNext/OnError/OnCompleted notifications.","triggerScenarios":"Calling source.Subscribe(observer, token) with a null IObserver<T> — e.g. an unassigned observer field, a custom observer factory returning null, or a cast/lookup that failed.","commonSituations":"Custom IObserver implementations not yet constructed before wiring up, DI container returning null for an observer dependency, or passing the result of a conditional that resolved to null.","solutions":["Pass a valid IObserver<T> instance; if you only need callbacks, use the Action-based overloads or AnonymousObserver.","Fix the factory/DI registration that produced a null observer.","Guard with a null check and a contextual exception before subscribing.","Prefer the observer-returning overload (Subscribe returns IDisposable) once a non-null observer exists."],"exampleFix":"// before\nIObserver<int> obs = observers.TryGet(\"agg\"); // may be null\nsource.Subscribe(obs, token);\n// after\nIObserver<int> obs = observers.TryGet(\"agg\") ?? new AnonymousObserver<int>(_ => { });\nif (obs == null) throw new InvalidOperationException(\"agg observer not built yet\");\nsource.Subscribe(obs, token);","handlingStrategy":"validation","validationCode":"if (observer is null) throw new InvalidOperationException(\"observer not constructed before Subscribe\");","typeGuard":"static bool HasObserver<T>(IObserver<T> observer) => observer is not null;","tryCatchPattern":"try { source.Subscribe(observer, token); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observer\")\n{ /* fall back to Action-based overloads */ }","preventionTips":["Prefer Action-based Subscribe overloads to avoid hand-rolled IObserver implementations","Register observers in DI before anything subscribes to them","Check TryGet-style lookups for null before use"],"tags":["null","argumentnull","observer","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-23T08:17:48.524Z"}