{"record":{"id":"a17006b330b6f2bf","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-nameof-dependencyobject","errorCode":null,"errorMessage":"throw new ArgumentNullException( nameof(dependencyObject));","messagePattern":"throw new ArgumentNullException\\( nameof\\(dependencyObject\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlAttributeProperties.cs","lineNumber":185,"sourceCode":"#region AttachedProperties\n\n        /// <summary>\n        /// xml:space Element property\n        /// </summary>\n        [Browsable(false)]\n        [Localizability(LocalizationCategory.NeverLocalize)]        \n        public static readonly DependencyProperty XmlSpaceProperty ;\n\n        /// <summary>\n        /// Return value of xml:space on the passed DependencyObject\n        /// </summary>\n        [DesignerSerializationOptions(DesignerSerializationOptions.SerializeAsAttribute)]\n        [AttachedPropertyBrowsableForType(typeof(DependencyObject))]\n        public static string GetXmlSpace(DependencyObject dependencyObject)\n        {\n            if (dependencyObject == null)\n            {\n                throw new ArgumentNullException( nameof(dependencyObject));\n            }\n\n            return (string)dependencyObject.GetValue(XmlSpaceProperty);\n        }\n\n        /// <summary>\n        /// Set value of xml:space on the passed DependencyObject\n        /// </summary>\n        public static void SetXmlSpace(DependencyObject dependencyObject, string value)\n        {\n            if (dependencyObject == null)\n            {\n                throw new ArgumentNullException( nameof(dependencyObject));\n            }\n\n            dependencyObject.SetValue(XmlSpaceProperty, value);\n        }\n","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlAttributeProperties.cs#L167-L203","documentation":"XmlAttributeProperties.GetXmlSpace(DependencyObject) reads the xml:space attached property and throws ArgumentNullException when dependencyObject is null. Reading an attached property requires a valid target object on which to call GetValue.","triggerScenarios":"Calling GetXmlSpace(null) — e.g. in a style/template callback or serialization pass where the sender/reference is null, or iterating a collection containing null entries.","commonSituations":"Custom serialization code inspecting whitespace handling per element where some items are null; event handlers firing for detached elements that were captured as null by the time the handler ran.","solutions":["Null-check the DependencyObject before calling GetXmlSpace","Filter null entries out of collections before the property inspection pass","When the element may be absent, decide on a default xml:space (e.g. \"default\") instead of reading"],"exampleFix":"// before\nvar space = XmlAttributeProperties.GetXmlSpace(item); // item may be null\n// after\nvar space = item != null ? XmlAttributeProperties.GetXmlSpace(item) : \"default\";","handlingStrategy":"type-guard","validationCode":"if (dependencyObject == null) return \"default\";\nvar space = XmlAttributeProperties.GetXmlSpace(dependencyObject);","typeGuard":"static bool CanReadXmlSpace(DependencyObject d) => d != null;","tryCatchPattern":"try { space = XmlAttributeProperties.GetXmlSpace(d); }\ncatch (ArgumentNullException ex) { space = \"default\"; Log.Warn(\"GetXmlSpace target was null\"); }","preventionTips":["Skip null entries when iterating element collections","Null-check elements in serialization/inspection passes"],"tags":["wpf","attached-property","argument-null"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}