{"record":{"id":"9947c1427b00c694","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-subscribe","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'subscribe')","messagePattern":"Value cannot be null\\. \\(Parameter 'subscribe'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/AnonymousObservable.cs","lineNumber":24,"sourceCode":"\nnamespace System.Reactive\n{\n    /// <summary>\n    /// Class to create an <see cref=\"IObservable{T}\"/> instance from a delegate-based implementation of the <see cref=\"IObservable{T}.Subscribe(IObserver{T})\"/> method.\n    /// </summary>\n    /// <typeparam name=\"T\">The type of the elements in the sequence.</typeparam>\n    public sealed class AnonymousObservable<T> : ObservableBase<T>\n    {\n        private readonly Func<IObserver<T>, IDisposable> _subscribe;\n\n        /// <summary>\n        /// Creates an observable sequence object from the specified subscription function.\n        /// </summary>\n        /// <param name=\"subscribe\"><see cref=\"IObservable{T}.Subscribe(IObserver{T})\"/> method implementation.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"subscribe\"/> is <c>null</c>.</exception>\n        public AnonymousObservable(Func<IObserver<T>, IDisposable> subscribe)\n        {\n            _subscribe = subscribe ?? throw new ArgumentNullException(nameof(subscribe));\n        }\n\n        /// <summary>\n        /// Calls the subscription function that was supplied to the constructor.\n        /// </summary>\n        /// <param name=\"observer\">Observer to send notifications to.</param>\n        /// <returns>Disposable object representing an observer's subscription to the observable sequence.</returns>\n        protected override IDisposable SubscribeCore(IObserver<T> observer)\n        {\n            return _subscribe(observer) ?? Disposable.Empty;\n        }\n    }\n}\n","sourceCodeStart":6,"sourceCodeEnd":38,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/AnonymousObservable.cs#L6-L38","documentation":"The AnonymousObservable constructor throws ArgumentNullException when the subscribe delegate (Func<IObserver<T>, IDisposable>) is null. AnonymousObservable is the internal base used by Observable.Create; the delegate is invoked on every Subscribe call, so a null delegate would crash at subscription time rather than construction — the constructor fails fast instead.","triggerScenarios":"Calling new AnonymousObservable<T>(null) directly, or Observable.Create<T>(null), or Observable.Create<T>(observer => null-action) where a variable holding the subscribe lambda is null.","commonSituations":"Factory methods that conditionally build the subscribe function and return null in a default branch; DI/config-driven observer pipelines where the handler was not registered; refactors that renamed a method leaving a null method group.","solutions":["Pass a real subscribe function: Observable.Create<int>(observer => { ...; return Disposable.Empty; })","Default to a no-op subscribe (return Disposable.Empty) when a handler is optional","Null-check the delegate at the factory boundary before calling Create"],"exampleFix":"// before\nvar obs = Observable.Create<int>(subscribeFn); // subscribeFn is null\n// after\nvar obs = Observable.Create<int>(subscribeFn ?? (_ => Disposable.Empty));","handlingStrategy":"type-guard","validationCode":"// C#\nif (subscribe is null)\n    subscribe = _ => Disposable.Empty;\nvar obs = Observable.Create(subscribe);","typeGuard":"bool HasSubscribe<T>(Func<IObserver<T>, IDisposable>? f) => f is not null;","tryCatchPattern":"try { var obs = Observable.Create<int>(subscribeFn); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"subscribe\")\n{\n    obs = Observable.Empty<int>();\n}","preventionTips":["Use inline lambdas with Observable.Create rather than nullable delegate variables","Default optional subscribe handlers to _ => Disposable.Empty","Register required handlers before building pipelines"],"tags":["rx","observable","create","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"}