dotnet/maui · error · ArgumentNullException
public ElementEventArgs(Element element) => Element = elemen
Error message
public ElementEventArgs(Element element) => Element = element ?? throw new ArgumentNullException(nameof(element));
What it means
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.
Source
Thrown at src/Controls/src/Core/Element/ElementEventArgs.cs:11
#nullable disable
using System;
namespace Microsoft.Maui.Controls
{
/// <summary>Provides data for events pertaining to a single <see cref="Microsoft.Maui.Controls.Element"/>.</summary>
public class ElementEventArgs : EventArgs
{
/// <summary>Constructs and initializes a new instance of the <see cref="Microsoft.Maui.Controls.ElementEventArgs"/> class.</summary>
/// <param name="element">The element relevant to the event.</param>
public ElementEventArgs(Element element) => Element = element ?? throw new ArgumentNullException(nameof(element));
}
}View on GitHub (pinned to f377ff1c5e)
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.
Example fix
// before
ChildAdded?.Invoke(this, new ElementEventArgs(null)); // throws
// after
if (child != null)
ChildAdded?.Invoke(this, new ElementEventArgs(child)); Defensive patterns
Strategy: validation
Validate before calling
if (element == null) throw new ArgumentNullException(nameof(element)); var args = new ElementEventArgs(element);
Try / catch
try { ChildAdded?.Invoke(this, new ElementEventArgs(child)); }
catch (ArgumentNullException) { /* child was null; skip raising */ } Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- _ = view ?? throw new ArgumentNullException(nameof(view));
- throw new ArgumentNullException(nameof(type));
- internal ElementTemplate(Func<object> loadTemplate) : this()
- No test assembly found.
- Unable to find the required services. Please add all the req
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/913892b496611f35.
Report an issue: GitHub.