{"record":{"id":"083351f4a01ef729","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-nameof-xmlnamespace-083351","errorCode":null,"errorMessage":"throw new ArgumentNullException( nameof(xmlNamespace));","messagePattern":"throw new ArgumentNullException\\( nameof\\(xmlNamespace\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs","lineNumber":368,"sourceCode":"            }\n            return null;\n        }\n\n#if !PBTCOMPILER\n        /// <summary>\n        /// Looks up the XML prefix corresponding to a namespaceuri\n        /// </summary>\n        /// <param name=\"xmlNamespace\">The namespaceuri to look up</param>\n        /// <returns>\n        /// string.Empty if the given namespace corresponds to the default namespace; \n        /// otherwise, the XML prefix corresponding to the given namespace, or null \n        /// if none exists.\n        /// </returns>\n        public string LookupPrefix(string xmlNamespace)\n        {\n            if (xmlNamespace == null)\n            {\n                throw new ArgumentNullException( nameof(xmlNamespace)); \n            }\n\n            if (_lastDecl > 0)\n            {\n                for (int thisDecl = _lastDecl-1; thisDecl >= 0; thisDecl--)\n                {\n                    if (_nsDeclarations[thisDecl].Uri == xmlNamespace)\n                        return _nsDeclarations[thisDecl].Prefix;         \n                }\n            }\n            return null;\n       }\n\n        /// <summary>\n        /// DefaultNamespace for easy Access.\n        /// </summary>\n        public string DefaultNamespace()\n        {","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs#L350-L386","documentation":"XmlnsDictionary.LookupPrefix(string) throws ArgumentNullException when the xmlNamespace argument is null. The method performs a reverse lookup (namespace to prefix), which is meaningless for a null namespace.","triggerScenarios":"Calling xmlnsDictionary.LookupPrefix(null), e.g. when the namespace string came from an unchecked API result or deserialized value.","commonSituations":"Reverse-mapping namespaces during XAML serialization when a namespace URI variable was never assigned; custom namespace resolver code forwarding null.","solutions":["Null-check the namespace string before calling LookupPrefix","Default null namespaces to a safe value (e.g. string.Empty) when a lookup result placeholder is needed"],"exampleFix":"// before\nvar prefix = dictionary.LookupPrefix(xmlNamespace);\n\n// after\nvar prefix = xmlNamespace != null ? dictionary.LookupPrefix(xmlNamespace) : null;","handlingStrategy":"type-guard","validationCode":"var prefix = xmlNamespace == null ? null : dictionary.LookupPrefix(xmlNamespace);","typeGuard":"bool HasNamespace(string ns) => !string.IsNullOrEmpty(ns);","tryCatchPattern":"try { prefix = dictionary.LookupPrefix(xmlNamespace); }\ncatch (ArgumentNullException) { prefix = null; }","preventionTips":["Null-check namespace strings before reverse lookup","Avoid forwarding unchecked API results directly into LookupPrefix","Default unassigned namespace fields to string.Empty"],"tags":["wpf","xaml","null-reference"],"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"}