{"record":{"id":"e272a1e6029167a3","repo":"dotnet/wpf","slug":"sr-collection-nonull-materialcollection","errorCode":null,"errorMessage":"SR.Collection_NoNull","messagePattern":"SR\\.Collection_NoNull","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/MaterialCollection.cs","lineNumber":138,"sourceCode":"\n        /// <summary>\n        ///     Returns the index of \"value\" in the list\n        /// </summary>\n        public int IndexOf(Material value)\n        {\n            ReadPreamble();\n\n            return _collection.IndexOf(value);\n        }\n\n        /// <summary>\n        ///     Inserts \"value\" into the list at the specified position\n        /// </summary>\n        public void Insert(int index, Material value)\n        {\n            if (value == null)\n            {\n                throw new System.ArgumentException(SR.Collection_NoNull);\n            }\n\n            WritePreamble();\n\n            OnFreezablePropertyChanged(/* oldValue = */ null, /* newValue = */ value);\n\n            _collection.Insert(index, value);\n            OnInsert(value);\n\n\n            ++_version;\n            WritePostscript();\n        }\n\n        /// <summary>\n        ///     Removes \"value\" from the list\n        /// </summary>\n        public bool Remove(Material value)","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/MaterialCollection.cs#L120-L156","documentation":"MaterialCollection.Insert (and the Add path that delegates to it) throws ArgumentException (SR.Collection_NoNull) when the supplied Material is null. WPF's Freezable collections reject null items because OnFreezablePropertyChanged requires a valid Freezable instance for change tracking.","triggerScenarios":"Calling collection.Insert(index, null) or ((IList)collection).Insert(index, someNullObject); also Add(null) which routes through the same null check.","commonSituations":"Populating a Model3D's material collection from data where a material failed to load (texture/brush lookup returned null); XAML resources that did not resolve; factory methods returning null on failure.","solutions":["Check for null before inserting: if (material != null) collection.Insert(index, material);","Use a fallback material (e.g. DiffuseMaterial with a default Brush) instead of inserting null.","Fix the material factory/loader so it either returns a valid Material or is skipped entirely."],"exampleFix":"// before\nmaterials.Insert(0, LoadMaterial(name)); // may return null\n// after\nvar m = LoadMaterial(name);\nif (m != null) materials.Insert(0, m);","handlingStrategy":"validation","validationCode":"// before Insert/Add\nif (material != null && index >= 0 && index <= materials.Count)\n{\n    materials.Insert(index, material);\n}","typeGuard":"static bool IsInsertable(Material m) => m != null;","tryCatchPattern":"try { materials.Insert(index, material); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"null\"))\n{\n    materials.Insert(index, new DiffuseMaterial(Brushes.Gray)); // fallback material\n}","preventionTips":["Null-check materials returned from loaders/factories before adding","Use fallback materials instead of null placeholders","Bind only non-null items in UI data binding to material collections"],"tags":["wpf","argument-exception","null-argument","collection"],"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"}