dotnet/wpf · error · ArgumentNullException

SR.Panel_NoNullChildren

Error message

SR.Panel_NoNullChildren

What it means

UIElementCollection.ValidateElement throws ArgumentNullException with resource Panel_NoNullChildren when a null element is passed to Add/Insert/Remove-style internal entry points. A Panel (or any UIElement parent) cannot have null children, so the collection rejects null up front. This is a defensive guard on the collection API itself.

Solutions

  1. Check the child for null before calling Add/Insert and skip or substitute a placeholder element.
  2. Fix whatever produces the null child (binding, FindName, resource lookup) so a real UIElement is created.
  3. If a child may legitimately be absent, use Visibility.Collapsed on an empty element instead of adding null.

Example fix

// before
panel.Children.Add(FindName("SaveButton") as Button);
// after
var btn = FindName("SaveButton") as Button;
if (btn != null) panel.Children.Add(btn);
Defensive patterns

Strategy: validation

Validate before calling

if (element != null) panel.Children.Add(element);

Type guard

bool IsAddable(UIElement e) => e is not null;

Prevention

When it happens

Trigger: Calling UIElementCollection.Add(null), Insert(null), or internal AddInternal/InsertInternal/SetInternal paths with a null UIElement on any Panel-derived container.

Common situations: A data-bound or factory-produced child resolves to null (e.g. a DataTemplate or binding yields null), or a variable holding a control is null because FindName/GetResource returned nothing before adding to a Grid/StackPanel/Canvas.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs:515

        protected void ClearLogicalParent(UIElement element)
        {
            _logicalParent?.RemoveLogicalChild(element);
        }

        /// <summary>
        /// Provides access to visual parent.
        /// </summary>
        internal UIElement VisualParent
        {
            get { return (_visualParent); }
        }

        // Helper function to validate element; will throw exceptions if problems are detected.
        private void ValidateElement(UIElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException(SR.Format(SR.Panel_NoNullChildren, this.GetType()));
            }
        }

        private void VerifyWriteAccess()
        {
            Panel p = _visualParent as Panel;
            if (p != null && p.IsDataBound)
            {
                throw new InvalidOperationException(SR.Panel_BoundPanel_NoChildren);
            }
        }

        internal FrameworkElement LogicalParent
        {
            get { return _logicalParent; }
        }

        private readonly VisualCollection _visualChildren;

View on GitHub (pinned to 81131a70a4)