dotnet/wpf · error · ArgumentNullException

throw new ArgumentNullException( nameof(dependencyObject));

Error message

throw new ArgumentNullException( nameof(dependencyObject));

What it means

XmlAttributeProperties.GetXmlSpace(DependencyObject) reads the xml:space attached property and throws ArgumentNullException when dependencyObject is null. Reading an attached property requires a valid target object on which to call GetValue.

Solutions

  1. Null-check the DependencyObject before calling GetXmlSpace
  2. Filter null entries out of collections before the property inspection pass
  3. When the element may be absent, decide on a default xml:space (e.g. "default") instead of reading

Example fix

// before
var space = XmlAttributeProperties.GetXmlSpace(item); // item may be null
// after
var space = item != null ? XmlAttributeProperties.GetXmlSpace(item) : "default";
Defensive patterns

Strategy: type-guard

Validate before calling

if (dependencyObject == null) return "default";
var space = XmlAttributeProperties.GetXmlSpace(dependencyObject);

Type guard

static bool CanReadXmlSpace(DependencyObject d) => d != null;

Try / catch

try { space = XmlAttributeProperties.GetXmlSpace(d); }
catch (ArgumentNullException ex) { space = "default"; Log.Warn("GetXmlSpace target was null"); }

Prevention

When it happens

Trigger: Calling GetXmlSpace(null) — e.g. in a style/template callback or serialization pass where the sender/reference is null, or iterating a collection containing null entries.

Common situations: Custom serialization code inspecting whitespace handling per element where some items are null; event handlers firing for detached elements that were captured as null by the time the handler ran.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a17006b330b6f2bf. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlAttributeProperties.cs:185

#region AttachedProperties

        /// <summary>
        /// xml:space Element property
        /// </summary>
        [Browsable(false)]
        [Localizability(LocalizationCategory.NeverLocalize)]        
        public static readonly DependencyProperty XmlSpaceProperty ;

        /// <summary>
        /// Return value of xml:space on the passed DependencyObject
        /// </summary>
        [DesignerSerializationOptions(DesignerSerializationOptions.SerializeAsAttribute)]
        [AttachedPropertyBrowsableForType(typeof(DependencyObject))]
        public static string GetXmlSpace(DependencyObject dependencyObject)
        {
            if (dependencyObject == null)
            {
                throw new ArgumentNullException( nameof(dependencyObject));
            }

            return (string)dependencyObject.GetValue(XmlSpaceProperty);
        }

        /// <summary>
        /// Set value of xml:space on the passed DependencyObject
        /// </summary>
        public static void SetXmlSpace(DependencyObject dependencyObject, string value)
        {
            if (dependencyObject == null)
            {
                throw new ArgumentNullException( nameof(dependencyObject));
            }

            dependencyObject.SetValue(XmlSpaceProperty, value);
        }

View on GitHub (pinned to 81131a70a4)