{"record":{"id":"8371178e27013b1a","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-nameof-xmlnamespace-837117","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":609,"sourceCode":"        {\n            if (IsReadOnly)\n            {\n                throw new InvalidOperationException(SR.ParserDictionarySealed);\n            }\n        }\n\n        /// <summary>\n        /// Helper to Add Namespace - Checks for prefix from rear.\n        ///  If exists : override it locally, if not Adds an entry.\n        /// </summary>\n        /// <param name=\"prefix\">prefix to add</param>\n        /// <param name=\"xmlNamespace\">namespace uri string to add</param>\n        private void AddNamespace(string prefix, string xmlNamespace)\n        {\n            CheckSealed();\n            \n            if (xmlNamespace == null)\n                throw new ArgumentNullException(nameof(xmlNamespace));\n\n            if (prefix == null)\n                throw new ArgumentNullException(nameof(prefix));\n\n            int lastScopeCount = _nsDeclarations[_lastDecl].ScopeCount;\n\n            if (_lastDecl > 0)\n            {\n                // Check the local scope for the given prefix\n                for (int thisDecl = _lastDecl-1; \n                     thisDecl >= 0 && _nsDeclarations[thisDecl].ScopeCount == lastScopeCount; \n                     thisDecl--)\n                {\n                    if (String.Equals(_nsDeclarations[thisDecl].Prefix, prefix))\n                    {\n                        // Redefine an existing namespace\n                        _nsDeclarations[thisDecl].Uri = xmlNamespace;\n                        return; ","sourceCodeStart":591,"sourceCodeEnd":627,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs#L591-L627","documentation":"XmlnsDictionary.AddNamespace(string, string) throws ArgumentNullException when the xmlNamespace or prefix argument is null. After CheckSealed, both parameters must be non-null strings because they define an XML namespace declaration (prefix -> URI).","triggerScenarios":"Calling AddNamespace(\"x\", null) or AddNamespace(null, ns) directly, or indirectly via Add(object, object) with a null value cast to string, or via the indexer setter with a null value.","commonSituations":"Registering namespaces from configuration where a URI entry was empty/null; passing null values through generic IDictionary helper code.","solutions":["Ensure the namespace URI and prefix are non-null before calling","Substitute string.Empty or a default namespace when a null URI is acceptable in your scenario","Validate configuration values feeding namespace registrations"],"exampleFix":"// before\ndictionary.AddNamespace(\"x\", configNs); // configNs may be null\n\n// after\nif (configNs != null)\n{\n    dictionary.AddNamespace(\"x\", configNs);\n}","handlingStrategy":"validation","validationCode":"if (prefix != null && xmlNamespace != null) dictionary.AddNamespace(prefix, xmlNamespace);","typeGuard":"bool CanAddNamespace(string prefix, string ns) => prefix != null && ns != null;","tryCatchPattern":"try { dictionary.AddNamespace(prefix, nsUri); }\ncatch (ArgumentNullException) { /* skip or log missing prefix/namespace */ }","preventionTips":["Null-check both prefix and namespace URI before registration","Sanitize configuration values that feed namespace registrations","Never pass null through IDictionary.Add; filter nulls at the helper boundary"],"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-22T01:17:13.364Z"}