dotnet/wpf · error · ArgumentNullException
ArgumentNullException(nameof(instance))
Error message
ArgumentNullException(nameof(instance))
What it means
ArgumentNullException thrown by the public XamlObjectEventArgs constructor when the instance parameter is null. The library requires every event-args object to carry a non-null XAML object instance, so it validates immediately at construction.
Solutions
- Pass a non-null object instance to the XamlObjectEventArgs constructor
- Check for null before constructing the event args and skip raising the event if there is no instance
- Use the internal overload path or a different notification mechanism when the object legitimately does not exist
Example fix
// before
var args = new XamlObjectEventArgs(obj); // obj may be null
// after
if (obj != null) { var args = new XamlObjectEventArgs(obj); handler(this, args); } Defensive patterns
Strategy: type-guard
Validate before calling
if (instance == null) throw new InvalidOperationException("Cannot raise XamlObjectEventArgs without an instance"); Type guard
static bool IsValidXObjectArgs(object instance) => instance is not null; // guard: if (!IsValidXObjectArgs(obj)) return; var args = new XamlObjectEventArgs(obj);
Try / catch
try { var args = new XamlObjectEventArgs(instance); handler(sender, args); }
catch (ArgumentNullException) { log.Error("XamlObjectEventArgs requires a non-null instance"); } Prevention
- Never construct event args for null objects — skip the event instead
- Null-check object instances before raising XAML object lifecycle events
- Keep the null check adjacent to event-raising code so refactors don't lose it
When it happens
Trigger: Calling new XamlObjectEventArgs(null) directly, or wiring XAML object events (e.g. XamlObjectWriter settings OnXObjectHandled/AfterProperties handlers) and constructing the args yourself with a null object.
Common situations: Custom XAML-writing pipelines that raise object lifecycle events for synthetic/placeholder objects that end up null; refactored event code where the object variable was not yet assigned when the event was raised.
Related errors
- ' '.' ' is a property without a getter and is not a valid…
- SR.EventArgIsNull
- ' '.' ' is a property without a getter and is not a valid…
- ' '.' ' is a property without a getter and is not a valid…
- ' ' is not a valid XAML member name.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a61376b9178db2a3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Events/XamlObjectEventArgs.cs:12
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
namespace System.Xaml
{
public class XamlObjectEventArgs : EventArgs
{
public XamlObjectEventArgs(object instance)
{
Instance = instance ?? throw new ArgumentNullException(nameof(instance));
}
internal XamlObjectEventArgs(object instance, Uri sourceBamlUri, int elementLineNumber, int elementLinePosition) :
this(instance)
{
SourceBamlUri = sourceBamlUri;
ElementLineNumber = elementLineNumber;
ElementLinePosition = elementLinePosition;
}
public object Instance { get; private set; }
public Uri SourceBamlUri { get; private set; }
public int ElementLineNumber { get; private set; }
public int ElementLinePosition { get; private set; }
}View on GitHub (pinned to 81131a70a4)