dotnet/wpf · error · System.ArgumentException

SR.Collection_NoNull

Error message

SR.Collection_NoNull

What it means

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.

Solutions

  1. Filter out null entries before adding: items.Where(i => i != null)
  2. Validate the input sequence for nulls before calling the Add/insert API
  3. Fix the data source so it produces non-null elements
  4. Catch ArgumentException if nulls are legitimate and handle them upstream

Example fix

// before
collection.AddRange(items); // items contains null
// after
collection.AddRange(items.Where(i => i != null));
Defensive patterns

Strategy: validation

Validate before calling

if (items == null || items.Any(i => i == null)) throw new ArgumentNullException(nameof(items));

Type guard

bool AllNotNull<T>(IEnumerable<T> items) => items != null && items.All(i => i != null);

Try / catch

try { collection.AddRange(items); }
catch (ArgumentException ex) { logCorruptInput(ex); }

Prevention

When it happens

Trigger: Adding a range of items via the generated icollectionOfT insert path where at least one element in the enumerated sequence is null.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/5e06fb478dd9f639. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/CollectionHelper.cs:243

        public static string Collection_CheckAllNotNullAndFirePropertyChanged(McgResource resource, string type, string collection)
        {
            if (resource.CollectionType.IsFreezable)
            {
                string onInsert = String.Empty;

                if (resource.IsCollectionOfHandles)
                {
                    onInsert = [[inline]]OnInsert(item);[[/inline]];
                }

                return
                    [[inline]]
                        foreach ([[type]] item in [[collection]])
                        {
                            if (item == null)
                            {
                                throw new System.ArgumentException(SR.Collection_NoNull);
                            }
                            OnFreezablePropertyChanged(/* oldValue = */ null, item);
                            [[onInsert]]
                        }
                    [[/inline]];
            }
            else
                return String.Empty;
        }

        public static string Collection_Add(McgResource resource, string type, string value, string index)
        {
            if (index != String.Empty)
            {

                index = [[inline]][[index]] = [[/inline]];
            }

View on GitHub (pinned to 81131a70a4)