{"record":{"id":"5e06fb478dd9f639","repo":"dotnet/wpf","slug":"sr-collection-nonull-collectionhelper","errorCode":null,"errorMessage":"SR.Collection_NoNull","messagePattern":"SR\\.Collection_NoNull","errorType":"exception","errorClass":"System.ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/CollectionHelper.cs","lineNumber":243,"sourceCode":"\n        public static string Collection_CheckAllNotNullAndFirePropertyChanged(McgResource resource, string type, string collection)\n        {\n            if (resource.CollectionType.IsFreezable)\n            {\n                string onInsert = String.Empty;\n\n                if (resource.IsCollectionOfHandles)\n                {\n                    onInsert = [[inline]]OnInsert(item);[[/inline]];\n                }\n\n                return\n                    [[inline]]\n                        foreach ([[type]] item in [[collection]])\n                        {\n                            if (item == null)\n                            {\n                                throw new System.ArgumentException(SR.Collection_NoNull);\n                            }\n                            OnFreezablePropertyChanged(/* oldValue = */ null, item);\n                            [[onInsert]]\n                        }\n                    [[/inline]];\n            }\n            else\n                return String.Empty;\n        }\n\n        public static string Collection_Add(McgResource resource, string type, string value, string index)\n        {\n            if (index != String.Empty)\n            {\n\n                index = [[inline]][[index]] = [[/inline]];\n            }\n","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/CollectionHelper.cs#L225-L261","documentation":"This SR resource backs an ArgumentException thrown by generated Freezable collection code (CollectionHelper.cs, Collection_CheckAllNotNullAndFirePropertyChanged) when a null item is found in a sequence inserted into the collection. Freezable collections require all elements to be non-null so change notification (OnFreezablePropertyChanged) can be wired per item.","triggerScenarios":"Adding a range of items via the generated icollectionOfT insert path where at least one element in the enumerated sequence is null.","commonSituations":"Passing an array/list built from external data where some entries are null; deserializing a collection with missing elements; LINQ chains that can emit null elements into AddRange-style APIs.","solutions":["Filter out null entries before adding: items.Where(i => i != null)","Validate the input sequence for nulls before calling the Add/insert API","Fix the data source so it produces non-null elements","Catch ArgumentException if nulls are legitimate and handle them upstream"],"exampleFix":"// before\ncollection.AddRange(items); // items contains null\n// after\ncollection.AddRange(items.Where(i => i != null));","handlingStrategy":"validation","validationCode":"if (items == null || items.Any(i => i == null)) throw new ArgumentNullException(nameof(items));","typeGuard":"bool AllNotNull<T>(IEnumerable<T> items) => items != null && items.All(i => i != null);","tryCatchPattern":"try { collection.AddRange(items); }\ncatch (ArgumentException ex) { logCorruptInput(ex); }","preventionTips":["Sanitize external data before inserting into Freezable collections","Filter nulls at the data boundary, not at the collection API"],"tags":["wpf","collection","null-argument","freezable"],"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"}