dotnet/wpf · error · ArgumentException

SR.Collection_NoNull

Error message

SR.Collection_NoNull

What it means

GeometryCollection.Insert throws ArgumentException with SR.Collection_NoNull when the caller attempts to insert a null Geometry into the collection. WPF Freezable collections disallow null items because items participate in inheritance context, change notification, and rendering, all of which require a valid instance.

Solutions

  1. Check for null before inserting and skip or substitute a default Geometry (e.g. Geometry.Empty).
  2. Initialize the null source variable or fix the upstream code that produced a null Geometry.
  3. Use Add with a guard clause so the failure point is clearer if you need to debug where nulls originate.

Example fix

// before
geometryCollection.Insert(0, maybeGeometry);
// after
if (maybeGeometry != null)
{
    geometryCollection.Insert(0, maybeGeometry);
}
Defensive patterns

Strategy: validation

Validate before calling

if (geometry == null) throw new ArgumentException("Cannot insert a null Geometry", nameof(geometry));
geometryCollection.Insert(index, geometry);

Type guard

bool IsValidGeometry(Geometry g) => g != null;

Prevention

When it happens

Trigger: Calling GeometryCollection.Insert(index, null) or any API that routes into Insert (e.g. IList.Insert) with a null value.

Common situations: Building geometry groups from optional data where a geometry variable is null because parsing or a lookup failed; passing results of a method that returns Geometry? straight into the collection.

Related errors


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

Appendix: source

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

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

            return _collection.IndexOf(value);
        }

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

View on GitHub (pinned to 81131a70a4)