dotnet/wpf · error · ArgumentException

SR.Collection_NoNull

Error message

SR.Collection_NoNull

What it means

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.

Solutions

  1. Check for null before inserting: if (material != null) collection.Insert(index, material);
  2. Use a fallback material (e.g. DiffuseMaterial with a default Brush) instead of inserting null.
  3. Fix the material factory/loader so it either returns a valid Material or is skipped entirely.

Example fix

// before
materials.Insert(0, LoadMaterial(name)); // may return null
// after
var m = LoadMaterial(name);
if (m != null) materials.Insert(0, m);
Defensive patterns

Strategy: validation

Validate before calling

// before Insert/Add
if (material != null && index >= 0 && index <= materials.Count)
{
    materials.Insert(index, material);
}

Type guard

static bool IsInsertable(Material m) => m != null;

Try / catch

try { materials.Insert(index, material); }
catch (ArgumentException ex) when (ex.Message.Contains("null"))
{
    materials.Insert(index, new DiffuseMaterial(Brushes.Gray)); // fallback material
}

Prevention

When it happens

Trigger: Calling collection.Insert(index, null) or ((IList)collection).Insert(index, someNullObject); also Add(null) which routes through the same null check.

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

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/MaterialCollection.cs:138

        /// <summary>
        ///     Returns the index of "value" in the list
        /// </summary>
        public int IndexOf(Material value)
        {
            ReadPreamble();

            return _collection.IndexOf(value);
        }

        /// <summary>
        ///     Inserts "value" into the list at the specified position
        /// </summary>
        public void Insert(int index, Material value)
        {
            if (value == null)
            {
                throw new System.ArgumentException(SR.Collection_NoNull);
            }

            WritePreamble();

            OnFreezablePropertyChanged(/* oldValue = */ null, /* newValue = */ value);

            _collection.Insert(index, value);
            OnInsert(value);


            ++_version;
            WritePostscript();
        }

        /// <summary>
        ///     Removes "value" from the list
        /// </summary>
        public bool Remove(Material value)

View on GitHub (pinned to 81131a70a4)