dotnet/wpf · error · ArgumentException

SR.Collection_NoNull

Error message

SR.Collection_NoNull

What it means

TransformCollection.Insert throws ArgumentException(Collection_NoNull) when inserting a null Transform at a given index. Like TextEffectCollection, TransformCollection is a Freezable collection that forbids null items because each element participates in Freezable change tracking.

Solutions

  1. Null-check the value before calling Insert
  2. If the null came from a FindResource call, use TryFindResource and skip missing values
  3. Filter nulls from batch insert operations

Example fix

// before
transformCollection.Insert(0, FindResource("MyTransform") as Transform); // may be null
// after
if (FindResource("MyTransform") is Transform t)
{
    transformCollection.Insert(0, t);
}
Defensive patterns

Strategy: validation

Validate before calling

if (value == null) throw new ArgumentNullException(nameof(value));

Type guard

static bool CanInsert(Transform t) => t != null;

Try / catch

try { collection.Insert(index, value); }
catch (ArgumentException ex) when (ex.Message.Contains("null"))
{ /* resolve or skip null transform */ }

Prevention

When it happens

Trigger: Calling Insert(index, null) or inserting via a non-generic IList with a null boxed value.

Common situations: Binding or resource lookups that resolve to null then get inserted; generic collection-manipulation helpers that don't null-check; XAML data that failed to produce the transform.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TransformCollection.cs:140

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

            return _collection.IndexOf(value);
        }

        /// <summary>
        ///     Inserts "value" into the list at the specified position
        /// </summary>
        public void Insert(int index, Transform 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(Transform value)

View on GitHub (pinned to 81131a70a4)