dotnet/wpf · error · ArgumentException

SR.Collection_NoNull

Error message

SR.Collection_NoNull

What it means

TextEffectCollection.Insert(int index, TextEffect value) throws ArgumentException(Collection_NoNull) when the value being inserted is null. Unlike some BCL collections that tolerate null reference-type elements, Freezable-derived WPF collections disallow null items because each element participates in the Freezable change/inheritance machinery (OnFreezablePropertyChanged).

Solutions

  1. Filter nulls before inserting: if (value != null) collection.Insert(i, value);
  2. Throw or log upstream so nulls never reach the collection call.
  3. Use OfType<TextEffect>() or Where(e => e != null) when bulk-inserting from a query.

Example fix

// before
TextEffect effect = FindEffect(name); // may be null
collection.Insert(0, effect); // ArgumentException Collection_NoNull
// after
TextEffect effect = FindEffect(name);
if (effect != null) collection.Insert(0, effect);
Defensive patterns

Strategy: type-guard

Validate before calling

if (value == null) throw new ArgumentNullException(nameof(value)); // before calling Insert

Type guard

static bool IsValidItem(object v) => v is TextEffect;

Try / catch

try { collection.Insert(i, effect); }
catch (ArgumentException ex) when (ex.Message.Contains("Collection_NoNull")) { /* skip or substitute a default TextEffect */ }

Prevention

When it happens

Trigger: Calling ((IList)textEffectCollection).Insert(i, null) or the typed Insert(i, (TextEffect)null) — commonly when the item came from an uninitialized variable or a failed lookup that returned null.

Common situations: Data pipelines that pass null placeholders for 'no effect'; LINQ queries that produce null entries; late-bound/reflection code inserting items into the non-generic IList.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TextEffectCollection.cs:128

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

            return _collection.IndexOf(value);
        }

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

            WritePreamble();

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

            _collection.Insert(index, value);



            ++_version;
            WritePostscript();
        }

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

View on GitHub (pinned to 81131a70a4)