{"record":{"id":"126f101c67f61934","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-prefix","errorCode":null,"errorMessage":"throw new ArgumentNullException( \"prefix\" );","messagePattern":"throw new ArgumentNullException\\( \"prefix\" \\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlAttributeProperties.cs","lineNumber":82,"sourceCode":"\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            \n            if (null != d)\n            {\n                return d[prefix];\n            }","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlAttributeProperties.cs#L64-L100","documentation":"XmlAttributeProperties.LookupNamespace throws ArgumentNullException when the prefix argument is null. The prefix is used both as a dictionary key in the element's XmlnsDictionary and to walk up the parent chain, so a null prefix is invalid input.","triggerScenarios":"Calling LookupNamespace(elem, null) — typically when the prefix was extracted from a parsed attribute name and the extraction produced null (e.g. an attribute without a prefix handled inconsistently).","commonSituations":"Custom XAML parsers or markup processing code splitting 'Prefix:LocalName' incorrectly; code that conflates the default namespace (empty string) with null.","solutions":["Pass string.Empty instead of null when the default (no-prefix) namespace is intended","Guard: if (prefix == null) prefix = string.Empty; before the call","Fix prefix extraction so unprefixed names consistently yield empty string"],"exampleFix":"// before\nvar ns = XmlAttributeProperties.LookupNamespace(element, attributeNamePrefix); // null for default ns\n// after\nvar ns = XmlAttributeProperties.LookupNamespace(element, attributeNamePrefix ?? string.Empty);","handlingStrategy":"validation","validationCode":"if (prefix == null) prefix = string.Empty;\nvar ns = XmlAttributeProperties.LookupNamespace(element, prefix);","typeGuard":"static bool IsValidPrefix(string p) => p != null;","tryCatchPattern":"try { ns = XmlAttributeProperties.LookupNamespace(element, prefix); }\ncatch (ArgumentNullException ex) { ns = null; Log.Warn(\"Prefix was null; use string.Empty for the default namespace\"); }","preventionTips":["Represent the default namespace as string.Empty, never null","Fix name-splitting logic so unprefixed attributes yield empty string"],"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-22T01:17:13.364Z"}