{"record":{"id":"cf383d99529345d3","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-target","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'target')","messagePattern":"Value cannot be null\\. \\(Parameter 'target'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Events.cs","lineNumber":549,"sourceCode":"        /// <para>\n        /// If no SynchronizationContext is present at the point of calling FromEventPattern, add and remove handler invocations are made synchronously on the thread\n        /// making the Subscribe or Dispose call, respectively.\n        /// </para>\n        /// <para>\n        /// It's recommended to lift FromEventPattern calls outside event stream query expressions due to the free-threaded nature of Reactive Extensions. Doing so\n        /// makes the captured SynchronizationContext predictable. This best practice also reduces clutter of bridging code inside queries, making the query expressions\n        /// more concise and easier to understand.\n        /// </para>\n        /// </remarks>\n        /// <seealso cref=\"ToEventPattern\"/>\n#if HAS_TRIMMABILITY_ATTRIBUTES\n        [RequiresUnreferencedCode(Constants_Core.EventReflectionTrimIncompatibilityMessage)]\n#endif\n        public static IObservable<EventPattern<object>> FromEventPattern(object target, string eventName)\n        {\n            if (target == null)\n            {\n                throw new ArgumentNullException(nameof(target));\n            }\n\n            if (eventName == null)\n            {\n                throw new ArgumentNullException(nameof(eventName));\n            }\n\n            return s_impl.FromEventPattern(target, eventName);\n        }\n\n        /// <summary>\n        /// Converts an instance .NET event, conforming to the standard .NET event pattern with an <see cref=\"EventArgs\"/> parameter, to an observable sequence.\n        /// Each event invocation is surfaced through an OnNext message in the resulting sequence.\n        /// Reflection is used to discover the event based on the target object type and the specified event name.\n        /// For conversion of events that don't conform to the standard .NET event pattern, use any of the FromEvent overloads instead.\n        /// </summary>\n        /// <param name=\"target\">Object instance that exposes the event to convert.</param>\n        /// <param name=\"eventName\">Name of the event to convert.</param>","sourceCodeStart":531,"sourceCodeEnd":567,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Events.cs#L531-L567","documentation":"The reflection-based overload Observable.FromEventPattern(object target, string eventName) throws ArgumentNullException when target is null. Rx uses the target instance to reflect over its events by name, so a null instance makes the operation impossible; it fails fast before any reflection work.","triggerScenarios":"Calling Observable.FromEventPattern(someObject, \"EventName\") with a null target reference while eventName is non-null (target check at line 549 precedes the eventName check at 554).","commonSituations":"A control or service field that is null because it was never assigned or injected (e.g. UI control accessed before InitializeComponent or after Dispose); a factory method that returned null; reflection-resolved object that didn't exist.","solutions":["Ensure the event source instance is created/assigned before building the observable, e.g. after InitializeComponent().","Pass the correct target object that actually owns the event.","If the source may legitimately be absent, guard with a null check and return Observable.Empty<EventPattern<object>>() or throw a descriptive error in your own code.","Verify DI/factory wiring so the target dependency is provided."],"exampleFix":"// before\nButton btn = null;\nvar clicks = Observable.FromEventPattern(btn, \"Click\");\n// after\nbtn = this.myButton; // ensure assigned\nvar clicks = Observable.FromEventPattern(btn, \"Click\");","handlingStrategy":"validation","validationCode":"if (target == null) throw new InvalidOperationException(\"Event source not initialized\");","typeGuard":"bool CanSubscribeFrom(object target, string eventName) => target != null && !string.IsNullOrEmpty(eventName);","tryCatchPattern":"try { var xs = Observable.FromEventPattern(target, eventName); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"target\") { /* fix source initialization */ }","preventionTips":["Create observables from events only after the owning object is constructed (e.g. after InitializeComponent).","Use Observable.Defer to delay target capture until subscription.","Assert non-null in debug builds at the call site.","Verify DI/factory wiring with startup integration tests."],"tags":["null-argument","rx","reflection","event-pattern"],"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"}