dotnet/wpf · error · ArgumentException

SR.Collection_NoNull

Error message

SR.Collection_NoNull

What it means

TextDecorationCollection.Insert rejects a null TextDecoration value and throws ArgumentException with SR.Collection_NoNull. WPF freezable collections do not permit null entries because downstream rendering and property-system code assumes every element is a valid TextDecoration. The check runs before WritePreamble so the collection is not mutated.

Solutions

  1. Check the value for null before calling Insert and skip or substitute a default TextDecoration
  2. Use Add/Insert only with non-null instances; construct a new TextDecoration() if you need a placeholder
  3. If the intent is 'no decoration', remove the entry or use TextDecorations prebuilt collections instead of inserting null

Example fix

// before
collection.Insert(0, GetDecoration());
// after
var d = GetDecoration();
if (d != null) collection.Insert(0, d);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0 || index > collection.Count) throw new ArgumentOutOfRangeException(nameof(index));
if (value == null) throw new ArgumentNullException(nameof(value));

Type guard

bool IsValidForInsert(int index, TextDecoration value) => value != null && index >= 0 && index <= collection.Count;

Prevention

When it happens

Trigger: Calling collection.Insert(index, null) on a TextDecorationCollection, or any API that forwards a null item into Insert (e.g. deserializing a XAML collection with a null entry).

Common situations: Building a TextDecorationCollection programmatically from a data source where some rows have no decoration; data binding that yields null items; XAML resource dictionaries that resolve to null.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs:119

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

            return _collection.IndexOf(value);
        }

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

View on GitHub (pinned to 81131a70a4)