dotnet/wpf · error · ArgumentNullException
throw new ArgumentNullException( "elem" );
Error message
throw new ArgumentNullException( "elem" );
What it means
XmlAttributeProperties.LookupNamespace(DependencyObject elem, string prefix) throws ArgumentNullException when elem is null. The method walks the dependency-object parent chain to resolve an XML namespace prefix, so it needs a valid starting element.
Solutions
- Verify the DependencyObject is created and non-null before calling LookupNamespace
- Fail fast with your own error message if elem is null
- If the element may legitimately be absent, skip the prefix lookup
Example fix
// before var ns = XmlAttributeProperties.LookupNamespace(element, prefix); // element may be null // after var ns = element != null ? XmlAttributeProperties.LookupNamespace(element, prefix) : null;
Defensive patterns
Strategy: type-guard
Validate before calling
if (element == null) return null; var ns = XmlAttributeProperties.LookupNamespace(element, prefix);
Type guard
static bool CanLookup(DependencyObject e) => e != null;
Try / catch
try { ns = XmlAttributeProperties.LookupNamespace(element, prefix); }
catch (ArgumentNullException ex) { Log.Warn("LookupNamespace needs a non-null element and prefix"); ns = null; } Prevention
- Null-check FindName results before namespace lookups
- Avoid lookups before the element tree is loaded
- Guard against detached/unloaded elements in designer extensions
When it happens
Trigger: Calling XmlAttributeProperties.LookupNamespace(null, "x") — e.g. passing a control that has not been created yet, or the result of a FindName lookup that returned null.
Common situations: XAML designer/parser extensions resolving prefixes during load before the element tree exists; data-binding code that resolves xmlns prefixes for dynamically generated attributes where the element reference is null.
Related errors
- throw new ArgumentNullException( "prefix" );
- throw new ArgumentNullException( "uri" );
- ArgumentNullException(nameof(assemblyName))
- ArgumentNullException(nameof(assemblyNames))
- ArgumentNullException(nameof(assemblyPath))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e12c465871a8f7d6.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlAttributeProperties.cs:78
XmlNamespaceMapsProperty =
DependencyProperty.RegisterAttached("XmlNamespaceMaps", typeof(Hashtable), typeof(XmlAttributeProperties),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
}
#if Lookups
/// <summary>
/// Looks up the namespace corresponding to an XML namespace prefix
/// </summary>
/// <param name="elem">The DependencyObject containing the namespace mappings to be
/// used in the lookup</param>
/// <param name="prefix">The XML namespace prefix to look up</param>
/// <returns>The namespace corresponding to the given prefix if it exists,
/// null otherwise</returns>
public static string LookupNamespace(DependencyObject elem, string prefix)
{
if (elem == null)
{
throw new ArgumentNullException( "elem" );
}
if (prefix == null)
{
throw new ArgumentNullException( "prefix" );
}
// Walk up the parent chain until one of the parents has the passed prefix in
// its xmlns dictionary, or until a null parent is reached.
XmlnsDictionary d = GetXmlnsDictionary(elem);
while ((null == d || null == d[prefix]) && null != elem)
{
elem = LogicalTreeHelper.GetParent(elem);
if (elem != null)
{
d = GetXmlnsDictionary(elem);
}
}
View on GitHub (pinned to 81131a70a4)