{"record":{"id":"a61376b9178db2a3","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-instance","errorCode":null,"errorMessage":"ArgumentNullException(nameof(instance))","messagePattern":"ArgumentNullException\\(nameof\\(instance\\)\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Events/XamlObjectEventArgs.cs","lineNumber":12,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT license.\n\n#nullable disable\n\nnamespace System.Xaml\n{\n    public class XamlObjectEventArgs : EventArgs\n    {\n        public XamlObjectEventArgs(object instance)\n        {\n            Instance = instance ?? throw new ArgumentNullException(nameof(instance));\n        }\n\n        internal XamlObjectEventArgs(object instance, Uri sourceBamlUri, int elementLineNumber, int elementLinePosition) :\n            this(instance)\n        {\n            SourceBamlUri = sourceBamlUri;\n            ElementLineNumber = elementLineNumber;\n            ElementLinePosition = elementLinePosition;\n        }\n\n        public object Instance { get; private set; }\n\n        public Uri SourceBamlUri { get; private set; }\n\n        public int ElementLineNumber { get; private set; }\n\n        public int ElementLinePosition { get; private set; }\n    }","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Events/XamlObjectEventArgs.cs#L1-L30","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nvar args = new XamlObjectEventArgs(obj); // obj may be null\n// after\nif (obj != null) { var args = new XamlObjectEventArgs(obj); handler(this, args); }","handlingStrategy":"type-guard","validationCode":"if (instance == null) throw new InvalidOperationException(\"Cannot raise XamlObjectEventArgs without an instance\");","typeGuard":"static bool IsValidXObjectArgs(object instance) => instance is not null;\n// guard: if (!IsValidXObjectArgs(obj)) return; var args = new XamlObjectEventArgs(obj);","tryCatchPattern":"try { var args = new XamlObjectEventArgs(instance); handler(sender, args); }\ncatch (ArgumentNullException) { log.Error(\"XamlObjectEventArgs requires a non-null instance\"); }","preventionTips":["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"],"tags":["argumentnull","xaml","events"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}