{"record":{"id":"8ce41a16298d7755","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-nameof-prefix-8ce41a","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(prefix));","messagePattern":"throw new ArgumentNullException\\(nameof\\(prefix\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs","lineNumber":612,"sourceCode":"                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; \n                    }\n                }\n","sourceCodeStart":594,"sourceCodeEnd":630,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs#L594-L630","documentation":"XmlnsDictionary.AddNamespace throws ArgumentNullException when the prefix argument is null. The dictionary maps XML namespace prefixes to namespace URIs for WPF XAML markup processing, and a null prefix has no meaning, so the library rejects it immediately after checking the dictionary is not sealed.","triggerScenarios":"Calling XmlnsDictionary.Add(prefix, xmlNamespace) (which forwards to AddNamespace) with a null prefix string, e.g. dictionary.Add(null, \"http://...\") or passing an uninitialized variable as the prefix.","commonSituations":"Building custom XAML namespaces programmatically, XAML schema context setup code, or deserializing prefix mappings from config where a prefix entry is missing.","solutions":["Ensure the prefix argument is a non-null string before calling Add/AddNamespace.","If the prefix may be absent, skip the Add call or substitute a sensible default (e.g. empty string for the default namespace).","Validate inputs when loading prefix mappings from external config before passing them in."],"exampleFix":"// before\nxmlnsDictionary.Add(prefix, namespaceUri);\n// after\nif (prefix == null) throw new InvalidOperationException(\"Prefix mapping missing from config\");\nxmlnsDictionary.Add(prefix, namespaceUri);","handlingStrategy":"validation","validationCode":"if (prefix == null) throw new ArgumentException(\"prefix must be non-null\", nameof(prefix));\nxmlnsDictionary.Add(prefix, namespaceUri);","typeGuard":"bool IsValidPrefix(string p) => p != null;","tryCatchPattern":"try { xmlnsDictionary.Add(prefix, ns); }\ncatch (ArgumentNullException ex) { /* log missing prefix; skip mapping */ }","preventionTips":["Null-check prefix/URI pairs loaded from config before registering them","Use string.Empty explicitly for the default namespace instead of null","Centralize namespace registration in one helper that validates inputs"],"tags":["wpf","xaml","null-argument","csharp"],"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"}