{"record":{"id":"2f0e6fe583d7dcee","repo":"dotnet/wpf","slug":"sr-collection-nonull-uielementcollection","errorCode":null,"errorMessage":"SR.Collection_NoNull","messagePattern":"SR\\.Collection_NoNull","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs","lineNumber":386,"sourceCode":"                _visualParent.InvalidateMeasure();\n            }\n        }\n\n\n        /// <summary>\n        /// Method that forwards to VisualCollection.Move\n        /// </summary>\n        /// <param name=\"visual\"></param>\n        /// <param name=\"destination\"></param>\n        internal void MoveVisualChild(Visual visual, Visual destination)\n        {\n            _visualChildren.Move(visual, destination);\n        }\n\t\t\n        private UIElement Cast(object value)\n        {\n            if (value == null)\n                throw new System.ArgumentException(SR.Format(SR.Collection_NoNull, \"UIElementCollection\"));\n\n            UIElement element = value as UIElement;\n\n            if (element == null)\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, \"UIElementCollection\", value.GetType().Name, \"UIElement\"));\n\n            return element;\n        }\n\t\t\n        #region IList Members\n\n        /// <summary>\n        /// Adds an element to the UIElementCollection\n        /// </summary>\n        int IList.Add(object value)\n        {\n            return Add(Cast(value));\n        }","sourceCodeStart":368,"sourceCodeEnd":404,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs#L368-L404","documentation":"UIElementCollection.Cast (used by Add, Insert and the constructor's IList paths) throws ArgumentException when a null value is supplied, because the collection does not permit null children. The message names SR.Collection_NoNull with the collection type.","triggerScenarios":"panel.Children.Add(null), panel.Children.Insert(0, null), or adding through an IList reference where the item is null.","commonSituations":"Data binding or factory methods that can return null; building UI from deserialized models where an element failed to instantiate; loop-generated children where a creation step returned null.","solutions":["Null-check the element before adding it to the collection.","Fix the factory/loader that returned null instead of a UIElement.","Use Children.Remove on placeholder elements rather than adding nulls."],"exampleFix":"// before\npanel.Children.Add(BuildElement()); // may return null\n// after\nvar e = BuildElement();\nif (e != null) panel.Children.Add(e);","handlingStrategy":"validation","validationCode":"if (element != null) panel.Children.Add(element);","typeGuard":"static bool IsAddable(UIElement e) => e != null;","tryCatchPattern":"try { panel.Children.Add(value); }\ncatch (ArgumentException ex) { /* null or wrong type; log and skip */ }","preventionTips":["Null-check factory results before adding to Children.","Fail fast in loaders that produce null elements.","Use Remove instead of adding nulls to clear items."],"tags":["wpf","argument-exception","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-22T01:17:13.364Z"}