{"record":{"id":"a2ffcd73f860b244","repo":"AvaloniaUI/Avalonia","slug":"cannot-raise-an-event-whose-routedevent-is-null","errorCode":null,"errorMessage":"Cannot raise an event whose RoutedEvent is null.","messagePattern":"Cannot raise an event whose RoutedEvent is null\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Interactivity/Interactive.cs","lineNumber":123,"sourceCode":"        /// <param name=\"handler\">The handler.</param>\n        public void RemoveHandler<TEventArgs>(RoutedEvent<TEventArgs> routedEvent, EventHandler<TEventArgs>? handler)\n            where TEventArgs : RoutedEventArgs\n        {\n            if (handler is not null)\n                RemoveHandler(routedEvent, (Delegate)handler);\n        }\n\n        /// <summary>\n        /// Raises a routed event.\n        /// </summary>\n        /// <param name=\"e\">The event args.</param>\n        public void RaiseEvent(RoutedEventArgs e)\n        {\n            e = e ?? throw new ArgumentNullException(nameof(e));\n\n            if (e.RoutedEvent == null)\n            {\n                throw new ArgumentException(\"Cannot raise an event whose RoutedEvent is null.\");\n            }\n\n            using var route = BuildEventRoute(e.RoutedEvent);\n            route.RaiseEvent(this, e);\n        }\n\n        /// <summary>\n        /// Builds an event route for a routed event.\n        /// </summary>\n        /// <param name=\"e\">The routed event.</param>\n        /// <returns>An <see cref=\"EventRoute\"/> describing the route.</returns>\n        /// <remarks>\n        /// Usually, calling <see cref=\"RaiseEvent(RoutedEventArgs)\"/> is sufficient to raise a routed\n        /// event, however there are situations in which the construction of the event args is expensive\n        /// and should be avoided if there are no handlers for an event. In these cases you can call\n        /// this method to build the event route and check the <see cref=\"EventRoute.HasHandlers\"/>\n        /// property to see if there are any handlers registered on the route. If there are, call\n        /// <see cref=\"EventRoute.RaiseEvent(Interactive, RoutedEventArgs)\"/> to raise the event.","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Interactivity/Interactive.cs#L105-L141","documentation":"RaiseEvent dispatches a RoutedEventArgs along the event route, which is built from e.RoutedEvent. If RoutedEvent is null the route cannot be constructed (there is no event identity), so RaiseEvent throws ArgumentException. This catches a common mistake: raising an args object that never had its event set.","triggerScenarios":"Calling `control.RaiseEvent(new RoutedEventArgs())` without assigning `.RoutedEvent = SomeClass.SomeEvent`. Passing a reused/blank RoutedEventArgs whose RoutedEvent is null.","commonSituations":"Constructing RoutedEventArgs via the parameterless constructor and forgetting to set RoutedEvent. Reusing an event-args pool object that was reset. Custom controls raising an event field that is null.","solutions":["Always set RoutedEvent before raising: `e.RoutedEvent = MyControl.SomeEvent; control.RaiseEvent(e);`.","Use the RoutedEventArgs constructor that takes the RoutedEvent: `new RoutedEventArgs(MyControl.SomeEvent)`.","Null-check `e.RoutedEvent` (or the static event field) before raising."],"exampleFix":"// before:\ncontrol.RaiseEvent(new RoutedEventArgs()); // RoutedEvent null -> throws\n\n// after:\ncontrol.RaiseEvent(new RoutedEventArgs(MyControl.TappedEvent));\n// or:\nvar e = new RoutedEventArgs { RoutedEvent = MyControl.TappedEvent };\ncontrol.RaiseEvent(e);","handlingStrategy":"validation","validationCode":"if (e.RoutedEvent is null)\n    throw new ArgumentException(\"RoutedEventArgs needs a RoutedEvent.\");\ncontrol.RaiseEvent(e);","typeGuard":"static bool HasEvent(RoutedEventArgs e) => e.RoutedEvent is not null;","tryCatchPattern":null,"preventionTips":["Use new RoutedEventArgs(MyControl.SomeEvent) to set the event at construction.","Never reuse/reset a RoutedEventArgs without resetting RoutedEvent.","Null-check static event fields before raising."],"tags":["events","routedevent","interactivity","argument"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}