{"record":{"id":"12f1ef6fbb2af5ef","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-nameof-xmlnsdictionary","errorCode":null,"errorMessage":"throw new ArgumentNullException( nameof(xmlnsDictionary));","messagePattern":"throw new ArgumentNullException\\( nameof\\(xmlnsDictionary\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs","lineNumber":96,"sourceCode":"        /// </summary>\n        public XmlnsDictionary()\n        {\n            // Initializes the XmlnsDictionary NamespaceDeclarations array with default 8 items\n            // and initializes the last Declaration marker to zero for first insertion.\n            Initialize();\n        }\n\n#if !PBTCOMPILER\n        /// <summary>\n        /// Construct an XmlnsDictionary based on an already existing one. The Sealed property is not\n        /// propagated between dictionaries.\n        /// </summary>\n        /// <param name=\"xmlnsDictionary\">The dictionary on which to base the new one</param>\n        public XmlnsDictionary(XmlnsDictionary xmlnsDictionary)\n        {\n            if(null == xmlnsDictionary)\n            {\n                throw new ArgumentNullException( nameof(xmlnsDictionary));\n            }\n            \n            // Copy the Declarations if they exists \n            if (xmlnsDictionary != null && xmlnsDictionary.Count > 0)\n            {\n                _lastDecl = xmlnsDictionary._lastDecl;\n                if (_nsDeclarations == null)\n                {\n                    _nsDeclarations = new NamespaceDeclaration[_lastDecl+1];\n                }\n\n                // Initialize the count to Zero and start counting.\n                _countDecl = 0;\n                \n                for (int i = 0; i <= _lastDecl; i++)\n                {\n                    // We copy the entire Dictionary, but update the count only for non-null uri/\n                    // The reason is, Count, doesn't make sense when we are implementing the ","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs#L78-L114","documentation":"The XmlnsDictionary copy constructor throws ArgumentNullException when the source XmlnsDictionary is null. A new dictionary is always built by copying from an existing instance, so a null source has no valid meaning.","triggerScenarios":"Calling new XmlnsDictionary((XmlnsDictionary)null), e.g. cloning a dictionary stored in a field or property that was never initialized.","commonSituations":"Copying a dictionary obtained from a ParserContext whose XmlnsDictionary property was not yet populated, or cloning before an object's dictionary member was assigned.","solutions":["Ensure the source XmlnsDictionary is instantiated before copying","Check for null before invoking the copy constructor and create a fresh empty XmlnsDictionary instead when the source is absent"],"exampleFix":"// before\nvar copy = new XmlnsDictionary(sourceDict);\n\n// after\nvar copy = sourceDict != null ? new XmlnsDictionary(sourceDict) : new XmlnsDictionary();","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"bool CanCopy(XmlnsDictionary source) => source != null;\nvar copy = CanCopy(source) ? new XmlnsDictionary(source) : new XmlnsDictionary();","tryCatchPattern":"try { var copy = new XmlnsDictionary(source); }\ncatch (ArgumentNullException) { var copy = new XmlnsDictionary(); }","preventionTips":["Initialize XmlnsDictionary fields at declaration or construction time","Null-check ParserContext.XmlnsDictionary sources before cloning","Prefer lazy initialization over uninitialized members"],"tags":["wpf","xaml","null-reference","constructor"],"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"}