{"record":{"id":"5da9fef8a0849636","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-uri","errorCode":null,"errorMessage":"throw new ArgumentNullException( \"uri\" );","messagePattern":"throw new ArgumentNullException\\( \"uri\" \\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlAttributeProperties.cs","lineNumber":126,"sourceCode":"        /// Looks up the XML prefix corresponding to a namespaceUri\n        /// </summary>\n        /// <param name=\"elem\">The DependencyObject containing the namespace mappings to \n        ///  be used in the lookup</param>\n        /// <param name=\"uri\">The namespaceUri to look up</param>\n        /// <returns>\n        /// string.Empty if the given namespace corresponds to the default namespace; otherwise, \n        /// the XML prefix corresponding to the given namespace, or null if none exists\n        /// </returns>\n        public static string LookupPrefix(DependencyObject elem, string uri)\n        {\n            DependencyObject oldElem = elem;\n            if (elem == null)\n            {\n                throw new ArgumentNullException( \"elem\" ); \n            }\n            if (uri == null)\n            {\n                throw new ArgumentNullException( \"uri\" ); \n            }\n\n            // Following the letter of the specification, the default namespace should take \n            // precedence in the case of redundancy, so we check that first.\n            if (DefaultNamespace(elem) == uri) \n                return string.Empty;\n\n            while (null != elem)\n            {\n                XmlnsDictionary d = GetXmlnsDictionary(elem);\n                if (null != d)\n                {\n                    // Search through all key/value pairs in the dictionary\n                    foreach (DictionaryEntry e in d)\n                    {\n                        if ((string)e.Value == uri && LookupNamespace(oldElem, (string)e.Key) == uri)\n                        {\n                            return (string)e.Key;","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlAttributeProperties.cs#L108-L144","documentation":"XmlAttributeProperties.LookupPrefix throws ArgumentNullException when the uri argument is null. The URI is compared against the element's default namespace and xmlns dictionaries; null is not a valid namespace URI here.","triggerScenarios":"Calling LookupPrefix(elem, null) — usually because the namespace URI came from XML attribute parsing where the xmlns attribute was absent, or a lookup that returned null (e.g. from GetNamespace on a missing prefix).","commonSituations":"Code that captures a namespace URI from an XmlReader/XmlNode and stores null for absent namespaces, then forwards it to LookupPrefix; schema-driven generation where optional namespaces were omitted.","solutions":["Only call LookupPrefix when the URI is a valid non-null string (use string.Empty for the default namespace)","Guard: if (string.IsNullOrEmpty(uri)) return null; before the call","Fix the upstream URI extraction to yield string.Empty rather than null"],"exampleFix":"// before\nvar prefix = XmlAttributeProperties.LookupPrefix(element, resolvedNamespaceUri); // may be null\n// after\nvar prefix = resolvedNamespaceUri != null\n    ? XmlAttributeProperties.LookupPrefix(element, resolvedNamespaceUri)\n    : null;","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(uri)) return null;\nvar prefix = XmlAttributeProperties.LookupPrefix(element, uri);","typeGuard":"static bool IsValidNamespaceUri(string u) => !string.IsNullOrEmpty(u);","tryCatchPattern":"try { prefix = XmlAttributeProperties.LookupPrefix(element, uri); }\ncatch (ArgumentNullException ex) { prefix = null; Log.Warn(\"Namespace URI was null\"); }","preventionTips":["Treat missing xmlns attributes as string.Empty, not null","Validate URIs extracted from XML nodes before forwarding"],"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"}