{"record":{"id":"913892b496611f35","repo":"dotnet/maui","slug":"public-elementeventargs-element-element-elemen","errorCode":null,"errorMessage":"public ElementEventArgs(Element element) => Element = element ?? throw new ArgumentNullException(nameof(element));","messagePattern":"public ElementEventArgs\\(Element element\\) => Element = element \\?\\? throw new ArgumentNullException\\(nameof\\(element\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Element/ElementEventArgs.cs","lineNumber":11,"sourceCode":"#nullable disable\nusing System;\n\nnamespace Microsoft.Maui.Controls\n{\n\t/// <summary>Provides data for events pertaining to a single <see cref=\"Microsoft.Maui.Controls.Element\"/>.</summary>\n\tpublic class ElementEventArgs : EventArgs\n\t{\n\t\t/// <summary>Constructs and initializes a new instance of the <see cref=\"Microsoft.Maui.Controls.ElementEventArgs\"/> class.</summary>\n\t\t/// <param name=\"element\">The element relevant to the event.</param>\n\t\tpublic ElementEventArgs(Element element) => Element = element ?? throw new ArgumentNullException(nameof(element));\n\t}\n}","sourceCodeStart":null,"sourceCodeEnd":null,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/ElementEventArgs.cs#L11","documentation":"Thrown by the ElementEventArgs constructor when the element argument is null. ElementEventArgs wraps a single Element for events like ChildAdded, ChildRemoved, DescendantAdded, DescendantRemoved. The constructor rejects null so event consumers can always rely on the Element property being non-null.","triggerScenarios":"Constructing new ElementEventArgs(null) directly, or framework/internal code raising child/descendant events with a null element reference (e.g., during cleanup when an element was already nulled out).","commonSituations":"Custom code or third-party libraries manually raising ChildAdded/ChildRemoved/DescendantAdded/DescendantRemoved events with null; element lifecycle bugs where an event is raised after the element reference was cleared.","solutions":["Pass a non-null Element when constructing ElementEventArgs.","If raising events in cleanup code, null-check the element before constructing the args.","Subscribe to ChildAdded/Removed etc. via the framework rather than constructing args manually."],"exampleFix":"// before\nChildAdded?.Invoke(this, new ElementEventArgs(null)); // throws\n// after\nif (child != null)\n    ChildAdded?.Invoke(this, new ElementEventArgs(child));","handlingStrategy":"validation","validationCode":"if (element == null) throw new ArgumentNullException(nameof(element));\nvar args = new ElementEventArgs(element);","typeGuard":null,"tryCatchPattern":"try { ChildAdded?.Invoke(this, new ElementEventArgs(child)); }\ncatch (ArgumentNullException) { /* child was null; skip raising */ }","preventionTips":["Null-check the element before constructing ElementEventArgs.","Raise child/descendant events through framework APIs that guarantee non-null references.","Avoid raising lifecycle events after the element reference has been cleared."],"tags":["maui","argumentnull","eventargs","element-lifecycle"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}