{"record":{"id":"cc64518c9a8c3390","repo":"stride3d/stride","slug":"cannot-add-a-null-uielement-to-the-children-list","errorCode":null,"errorMessage":"Cannot add a null UIElement to the children list.","messagePattern":"Cannot add a null UIElement to the children list\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.UI/Panels/Panel.cs","lineNumber":159,"sourceCode":"        {\n            if (oldElement.Parent == null)\n                throw new UIInternalException(\"The parent of the removed children UIElement not null\");\n            SetParent(oldElement, null);\n            SetVisualParent(oldElement, null);\n\n            if (oldElement.MouseOverState != MouseOverState.MouseOverNone)\n                MouseOverState = MouseOverState.MouseOverNone;\n        }\n\n        /// <summary>\n        /// Action to perform when a logical child is added.\n        /// </summary>\n        /// <param name=\"newElement\">The element that has been added</param>\n        /// <param name=\"index\">The index in the collection where the child has been added</param>\n        protected virtual void OnLogicalChildAdded(UIElement newElement, int index)\n        {\n            if (newElement == null)\n                throw new InvalidOperationException(\"Cannot add a null UIElement to the children list.\");\n            SetParent(newElement, this);\n            SetVisualParent(newElement, this);\n            VisualChildrenCollection.Sort(PanelChildrenSorter);\n            if (Children.Count > childrenArrangeWorldMatrix.Length)\n                childrenArrangeWorldMatrix = new Matrix[2 * Children.Count];\n        }\n\n        protected override void UpdateWorldMatrix(ref Matrix parentWorldMatrix, bool parentWorldChanged)\n        {\n            var shouldUpdateAllChridrenMatrix = parentWorldChanged || ArrangeChanged || LocalMatrixChanged;\n\n            base.UpdateWorldMatrix(ref parentWorldMatrix, parentWorldChanged);\n\n            var childIndex = 0;\n            foreach (var child in VisualChildrenCollection)\n            {\n                var shouldUpdateChildWorldMatrix = shouldUpdateAllChridrenMatrix || childrenWithArrangeMatrixInvalidated.Contains(child);\n                {","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.UI/Panels/Panel.cs#L141-L177","documentation":"Panel.OnLogicalChildAdded rejects null children with InvalidOperationException, because a null child would corrupt the parent/visual-parent links, sorting, and world-matrix arrays that Panel maintains for its children.","triggerScenarios":"Adding null to a panel's Children collection (e.g. panel.Children.Add(null)) or inserting null via a collection initializer / deserializer, which triggers LogicalChildrenChanged and then OnLogicalChildAdded.","commonSituations":"UI built from data where an item is null (missing model binding); deserializing a UI layout file containing a null node; LINQ producing null entries that get bulk-added.","solutions":["Filter null elements before adding to Children (use AddRange on a filtered list)","Fix the data source so items are never null (null-check models during binding)","If a child failed to construct, skip it or substitute a placeholder element instead of adding null"],"exampleFix":"// before\nforeach (var item in items)\n    panel.Children.Add(CreateChild(item)); // CreateChild may return null\n// after\nforeach (var item in items)\n{\n    var child = CreateChild(item);\n    if (child != null)\n        panel.Children.Add(child);\n}","handlingStrategy":"validation","validationCode":"var validChildren = items.Select(CreateChild).Where(c => c != null);\nforeach (var child in validChildren) panel.Children.Add(child);","typeGuard":"static bool IsValidChild(UIElement el) => el != null;","tryCatchPattern":"try { panel.Children.Add(child); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"null UIElement\")) { logger.LogWarning(\"Skipped null child element\"); }","preventionTips":["Filter null items before bulk-adding to Children","Ensure factory/binding functions never return null for UI items","Fix deserializers to skip or replace null UI nodes"],"tags":["csharp","ui","null-argument","panel"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}