dotnet/wpf · error · ArgumentNullException

ObjectKeyFrameCollection

Error message

ObjectKeyFrameCollection[{0}]

What it means

The ObjectKeyFrameCollection indexer setter throws ArgumentNullException when a null key frame is assigned. The paramName is the formatted string "ObjectKeyFrameCollection[index]" identifying the collection position.

Solutions

  1. Null-check before assigning into the collection
  2. Add only non-null ObjectKeyFrame instances
  3. Remove the frame (RemoveAt) rather than assigning null

Example fix

// before
frames[1] = null; // to clear
// after
frames.RemoveAt(1);
Defensive patterns

Strategy: validation

Validate before calling

if (frame == null) throw new ArgumentException($"ObjectKeyFrameCollection[{index}] requires a non-null frame");
collection[index] = frame;

Type guard

bool IsValidFrame(ObjectKeyFrame f) => f != null;

Try / catch

try { collection[index] = frame; }
catch (ArgumentNullException ex) when (ex.ParamName?.StartsWith("ObjectKeyFrameCollection[") == true) { /* assign a valid frame or remove the slot */ }

Prevention

When it happens

Trigger: Executing collection[index] = null on an ObjectKeyFrameCollection.

Common situations: Data-driven animation setups where frames may be null; replacing frames by index in code.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/ObjectKeyFrameCollection.cs:466

            }
        }

        /// <summary>
        /// Gets or sets the ObjectKeyFrame at a given index.
        /// </summary>
        public ObjectKeyFrame this[int index]
        {
            get
            {
                ReadPreamble();

                return _keyFrames[index];
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException(String.Format(CultureInfo.InvariantCulture, "ObjectKeyFrameCollection[{0}]", index));
                }

                WritePreamble();

                if (value != _keyFrames[index])
                {
                    OnFreezablePropertyChanged(_keyFrames[index], value);
                    _keyFrames[index] = value;

                    Debug.Assert(_keyFrames[index] != null);

                    WritePostscript();
                }
            }
        }

        #endregion
    }

View on GitHub (pinned to 81131a70a4)