{"record":{"id":"48856476ae12b3c4","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-localname","errorCode":null,"errorMessage":"ArgumentNullException(nameof(localName))","messagePattern":"ArgumentNullException\\(nameof\\(localName\\)\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs","lineNumber":109,"sourceCode":"        ///     you would call XamlTypeMapper.GetType(\"AvalonBase\",\"Button\");\n        ///     <para/>\n        ///     Note the XmlNamespace \"AvalonBase\" is the actual namespace value, not\n        ///     the base: prefix.\n        /// </remarks>\n        /// <param name=\"xmlNamespace\">NamespaceURI of tag</param>\n        /// <param name=\"localName\">localName of the Tag</param>\n        /// <returns>Type for the object. If no type was found NULL is returned</returns>\n        public Type GetType(\n            string xmlNamespace,\n            string localName)\n        {\n            if(null == xmlNamespace)\n            {\n                throw new ArgumentNullException( nameof(xmlNamespace));\n            }\n            if(null == localName)\n            {\n                throw new ArgumentNullException( nameof(localName));\n            }\n\n            TypeAndSerializer typeAndSerializer =\n                GetTypeOnly(xmlNamespace,localName);\n\n            return typeAndSerializer?.ObjectType;\n        }\n\n#if !PBTCOMPILER\n        /// <summary>\n        ///  Programmatic counterpart to the <?Mapping ... ?> XAML PI.  For example, <para/>\n        ///    <?Mapping XmlNamespace=\"swc\" ClrNamespace=\"System.Windows.ComponentModel\" Assembly=\"PresentationFramework\" ?>\n        /// </summary>\n        /// <param name=\"xmlNamespace\">\n        /// The \"swc\" argument in the mapping PI example.\n        /// </param>\n        /// <param name=\"clrNamespace\">\n        /// The \"System.Windows.ComponentModel\" argument in the mapping PI example.","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs#L91-L127","documentation":"XamlTypeMapper.GetType(xmlNamespace, localName) also requires a non-null localName (the type name without namespace prefix, e.g. \"Button\"). It throws ArgumentNullException for a null localName after validating xmlNamespace. Without a local name there is no type to look up in the namespace map.","triggerScenarios":"Calling mapper.GetType(xmlNamespace, null) — the local type name is null, commonly when XAML node streams supply an empty/unnamed element or when GetTypeFromBaseString/GetTypeArgsType forwards an uninitialized name.","commonSituations":"Processing XAML where a start element name failed to parse, custom BAML-style node processing that splits qualified names incorrectly (prefix parsing yields null local part), or reflection-driven code generation passing uninitialized strings.","solutions":["Validate/split the qualified name so localName is always non-null before calling GetType (e.g. name.Substring after the ':' separator, or the whole string when no prefix).","Skip elements whose local name is null/empty instead of resolving them.","Catch ArgumentNullException and surface a message identifying the malformed XAML element name."],"exampleFix":"// before\nType t = mapper.GetType(xmlns, SplitLocalName(qname)); // may return null\n// after\nstring localName = SplitLocalName(qname);\nif (string.IsNullOrEmpty(localName))\n    throw new XamlParseException($\"Invalid element name '{qname}'\");\nType t = mapper.GetType(xmlns, localName);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(localName))\n    throw new XamlParseException($\"Missing local name for element in namespace '{xmlNamespace}'\");","typeGuard":"static bool HasLocalName(string qualifiedName, out string localName)\n{\n    int i = qualifiedName?.IndexOf(':') ?? -1;\n    localName = i >= 0 ? qualifiedName.Substring(i + 1) : qualifiedName;\n    return !string.IsNullOrEmpty(localName);\n}","tryCatchPattern":"try\n{\n    type = mapper.GetType(xmlNamespace, localName);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"localName\")\n{\n    throw new XamlParseException(\"XAML element has an empty local name\", ex);\n}","preventionTips":["Split prefix:localName pairs defensively, defaulting the local part to the full name","Skip XAML nodes with empty or whitespace names before type resolution","Assert non-null in custom node-stream processing before calling the mapper"],"tags":["wpf","xaml","argument-null","type-resolution"],"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"}