{"record":{"id":"e12c465871a8f7d6","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-elem","errorCode":null,"errorMessage":"throw new ArgumentNullException( \"elem\" );","messagePattern":"throw new ArgumentNullException\\( \"elem\" \\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlAttributeProperties.cs","lineNumber":78,"sourceCode":"            XmlNamespaceMapsProperty =\n                DependencyProperty.RegisterAttached(\"XmlNamespaceMaps\", typeof(Hashtable), typeof(XmlAttributeProperties),\n                                            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));\n        }\n\n#if Lookups\n        /// <summary>\n        /// Looks up the namespace corresponding to an XML namespace prefix\n        /// </summary>\n        /// <param name=\"elem\">The DependencyObject containing the namespace mappings to be \n        ///  used in the lookup</param>\n        /// <param name=\"prefix\">The XML namespace prefix to look up</param>\n        /// <returns>The namespace corresponding to the given prefix if it exists, \n        /// null otherwise</returns>\n        public static string LookupNamespace(DependencyObject elem, string prefix)\n        {\n            if (elem == null)\n            {\n                throw new ArgumentNullException( \"elem\" ); \n            }\n            if (prefix == null)\n            {\n                throw new ArgumentNullException( \"prefix\" ); \n            }\n\n            // Walk up the parent chain until one of the parents has the passed prefix in \n            // its xmlns dictionary, or until a null parent is reached.\n            XmlnsDictionary d = GetXmlnsDictionary(elem);\n            while ((null == d || null == d[prefix]) && null != elem)\n            {\n                elem = LogicalTreeHelper.GetParent(elem);\n                if (elem != null)\n                {\n                    d = GetXmlnsDictionary(elem);\n                }\n            }\n            ","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlAttributeProperties.cs#L60-L96","documentation":"XmlAttributeProperties.LookupNamespace(DependencyObject elem, string prefix) throws ArgumentNullException when elem is null. The method walks the dependency-object parent chain to resolve an XML namespace prefix, so it needs a valid starting element.","triggerScenarios":"Calling XmlAttributeProperties.LookupNamespace(null, \"x\") — e.g. passing a control that has not been created yet, or the result of a FindName lookup that returned null.","commonSituations":"XAML designer/parser extensions resolving prefixes during load before the element tree exists; data-binding code that resolves xmlns prefixes for dynamically generated attributes where the element reference is null.","solutions":["Verify the DependencyObject is created and non-null before calling LookupNamespace","Fail fast with your own error message if elem is null","If the element may legitimately be absent, skip the prefix lookup"],"exampleFix":"// before\nvar ns = XmlAttributeProperties.LookupNamespace(element, prefix); // element may be null\n// after\nvar ns = element != null ? XmlAttributeProperties.LookupNamespace(element, prefix) : null;","handlingStrategy":"type-guard","validationCode":"if (element == null) return null;\nvar ns = XmlAttributeProperties.LookupNamespace(element, prefix);","typeGuard":"static bool CanLookup(DependencyObject e) => e != null;","tryCatchPattern":"try { ns = XmlAttributeProperties.LookupNamespace(element, prefix); }\ncatch (ArgumentNullException ex) { Log.Warn(\"LookupNamespace needs a non-null element and prefix\"); ns = null; }","preventionTips":["Null-check FindName results before namespace lookups","Avoid lookups before the element tree is loaded","Guard against detached/unloaded elements in designer extensions"],"tags":["wpf","xaml","namespace","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-21T21:30:21.729Z"}