dotnet/wpf · error · ArgumentNullException
ArgumentNullException(nameof(localName))
Error message
ArgumentNullException(nameof(localName))
What it means
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.
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.
Example fix
// before
Type t = mapper.GetType(xmlns, SplitLocalName(qname)); // may return null
// after
string localName = SplitLocalName(qname);
if (string.IsNullOrEmpty(localName))
throw new XamlParseException($"Invalid element name '{qname}'");
Type t = mapper.GetType(xmlns, localName); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(localName))
throw new XamlParseException($"Missing local name for element in namespace '{xmlNamespace}'"); Type guard
static bool HasLocalName(string qualifiedName, out string localName)
{
int i = qualifiedName?.IndexOf(':') ?? -1;
localName = i >= 0 ? qualifiedName.Substring(i + 1) : qualifiedName;
return !string.IsNullOrEmpty(localName);
} Try / catch
try
{
type = mapper.GetType(xmlNamespace, localName);
}
catch (ArgumentNullException ex) when (ex.ParamName == "localName")
{
throw new XamlParseException("XAML element has an empty local name", ex);
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- ArgumentNullException(nameof(xmlNamespace))
- throw new ArgumentNullException( nameof(element));
- throw new ArgumentNullException( nameof(typeName));
- ArgumentNullException(nameof(assemblyName))
- ArgumentNullException(nameof(assemblyNames))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/48856476ae12b3c4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs:109
/// you would call XamlTypeMapper.GetType("AvalonBase","Button");
/// <para/>
/// Note the XmlNamespace "AvalonBase" is the actual namespace value, not
/// the base: prefix.
/// </remarks>
/// <param name="xmlNamespace">NamespaceURI of tag</param>
/// <param name="localName">localName of the Tag</param>
/// <returns>Type for the object. If no type was found NULL is returned</returns>
public Type GetType(
string xmlNamespace,
string localName)
{
if(null == xmlNamespace)
{
throw new ArgumentNullException( nameof(xmlNamespace));
}
if(null == localName)
{
throw new ArgumentNullException( nameof(localName));
}
TypeAndSerializer typeAndSerializer =
GetTypeOnly(xmlNamespace,localName);
return typeAndSerializer?.ObjectType;
}
#if !PBTCOMPILER
/// <summary>
/// Programmatic counterpart to the <?Mapping ... ?> XAML PI. For example, <para/>
/// <?Mapping XmlNamespace="swc" ClrNamespace="System.Windows.ComponentModel" Assembly="PresentationFramework" ?>
/// </summary>
/// <param name="xmlNamespace">
/// The "swc" argument in the mapping PI example.
/// </param>
/// <param name="clrNamespace">
/// The "System.Windows.ComponentModel" argument in the mapping PI example.View on GitHub (pinned to 81131a70a4)