{"record":{"id":"fab63302146eaa40","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observer-producer","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'observer')","messagePattern":"Value cannot be null\\. \\(Parameter 'observer'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Internal/Producer.cs","lineNumber":34,"sourceCode":"        IDisposable SubscribeRaw(IObserver<TSource> observer, bool enableSafeguard);\n    }\n\n    /// <summary>\n    /// Base class for implementation of query operators, providing performance benefits over the use of Observable.Create.\n    /// </summary>\n    /// <typeparam name=\"TSource\">Type of the resulting sequence's elements.</typeparam>\n    internal abstract class BasicProducer<TSource> : IProducer<TSource>\n    {\n        /// <summary>\n        /// Publicly visible Subscribe method.\n        /// </summary>\n        /// <param name=\"observer\">Observer to send notifications on. The implementation of a producer must ensure the correct message grammar on the observer.</param>\n        /// <returns>IDisposable to cancel the subscription. This causes the underlying sink to be notified of unsubscription, causing it to prevent further messages from being sent to the observer.</returns>\n        public IDisposable Subscribe(IObserver<TSource> observer)\n        {\n            if (observer == null)\n            {\n                throw new ArgumentNullException(nameof(observer));\n            }\n\n            return SubscribeRaw(observer, enableSafeguard: true);\n        }\n\n        public IDisposable SubscribeRaw(IObserver<TSource> observer, bool enableSafeguard)\n        {\n            IDisposable run;\n            ISafeObserver<TSource>? safeObserver = null;\n\n            //\n            // See AutoDetachObserver.cs for more information on the safeguarding requirement and\n            // its implementation aspects.\n            //\n            if (enableSafeguard)\n            {\n                observer = safeObserver = SafeObserver<TSource>.Wrap(observer);\n            }","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Internal/Producer.cs#L16-L52","documentation":"Producer<TSource>.Subscribe(IObserver<TSource>) validates its observer argument and throws ArgumentNullException when null. Producer is the base class for all observable factories in System.Reactive, so subscribing a null observer is rejected immediately before any subscription machinery runs.","triggerScenarios":"Calling source.Subscribe(null) or passing a null IObserver<TSource> variable/return value of a factory method into Subscribe.","commonSituations":"A method that builds observers conditionally returns null on some code path; refactoring changed a lambda to an IObserver variable that was never assigned; interop code casts fail silently to null.","solutions":["Ensure a valid non-null IObserver<TSource> is passed; create one (e.g. Observer.Create<TSource>(...) or AnonymousObserver) instead of null.","Guard the observer argument at the call site and handle the null case before subscribing.","If you intended 'do nothing', use Observer.Empty<TSource>() or a no-op observer, not null."],"exampleFix":"// before\nIObserver<int> obs = condition ? BuildObserver() : null;\nsource.Subscribe(obs);\n// after\nIObserver<int> obs = condition ? BuildObserver() : Observer.Create<int>(_ => { });\nsource.Subscribe(obs);","handlingStrategy":"validation","validationCode":"if (observer == null) throw new ArgumentException(\"observer must not be null\");\nsource.Subscribe(observer);","typeGuard":"bool IsValid<T>(IObserver<T>? o) => o is not null;\nif (IsValid(observer)) source.Subscribe(observer);","tryCatchPattern":"try { source.Subscribe(observer); } catch (ArgumentNullException ex) when (ex.ParamName == \"observer\") { /* supply fallback observer */ }","preventionTips":["Never store observers in nullable variables that can be passed to Subscribe.","Use Observer.Create / action-based Subscribe overloads which cannot be null.","Enable nullable reference types so null flows are caught at compile time."],"tags":["null-argument","argument-validation","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-15T23:17:13.987Z"}