{"record":{"id":"e1c81913e96c258e","repo":"dotnet/wpf","slug":"sr-panel-nonullchildren","errorCode":null,"errorMessage":"SR.Panel_NoNullChildren","messagePattern":"SR\\.Panel_NoNullChildren","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs","lineNumber":515,"sourceCode":"        protected void ClearLogicalParent(UIElement element)\n        {\n            _logicalParent?.RemoveLogicalChild(element);\n        }\n\n        /// <summary>\n        /// Provides access to visual parent.\n        /// </summary>\n        internal UIElement VisualParent\n        {\n            get { return (_visualParent); }\n        }\n\n        // Helper function to validate element; will throw exceptions if problems are detected.\n        private void ValidateElement(UIElement element)\n        {\n            if (element == null)\n            {\n                throw new ArgumentNullException(SR.Format(SR.Panel_NoNullChildren, this.GetType()));\n            }\n        }\n\n        private void VerifyWriteAccess()\n        {\n            Panel p = _visualParent as Panel;\n            if (p != null && p.IsDataBound)\n            {\n                throw new InvalidOperationException(SR.Panel_BoundPanel_NoChildren);\n            }\n        }\n\n        internal FrameworkElement LogicalParent\n        {\n            get { return _logicalParent; }\n        }\n\n        private readonly VisualCollection _visualChildren;","sourceCodeStart":497,"sourceCodeEnd":533,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs#L497-L533","documentation":"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.","triggerScenarios":"Calling UIElementCollection.Add(null), Insert(null), or internal AddInternal/InsertInternal/SetInternal paths with a null UIElement on any Panel-derived container.","commonSituations":"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.","solutions":["Check the child for null before calling Add/Insert and skip or substitute a placeholder element.","Fix whatever produces the null child (binding, FindName, resource lookup) so a real UIElement is created.","If a child may legitimately be absent, use Visibility.Collapsed on an empty element instead of adding null."],"exampleFix":"// before\npanel.Children.Add(FindName(\"SaveButton\") as Button);\n// after\nvar btn = FindName(\"SaveButton\") as Button;\nif (btn != null) panel.Children.Add(btn);","handlingStrategy":"validation","validationCode":"if (element != null) panel.Children.Add(element);","typeGuard":"bool IsAddable(UIElement e) => e is not null;","tryCatchPattern":null,"preventionTips":["Null-check children resolved via FindName/resources before adding","Never add results of bindings directly; validate first"],"tags":["wpf","argument-null","uielementcollection"],"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"}