dotnet/wpf · error · ArgumentNullException
ColorKeyFrameCollection
Error message
ColorKeyFrameCollection[{0}] What it means
ColorKeyFrameCollection's public indexer setter throws ArgumentNullException when assigning null at a given index. The parameter name is built as "ColorKeyFrameCollection[{index}]" so the message points at the offending slot. The collection requires every element to be a non-null ColorKeyFrame.
Solutions
- Assign a valid ColorKeyFrame instance instead of null
- Use RemoveAt(index) to delete a slot rather than setting null
- Validate the key frame for null before the assignment
- Fix binding/source data so it never yields null key frames
Example fix
// before keyFrameCollection[0] = null; // ArgumentNullException // after if (newFrame != null) keyFrameCollection[0] = newFrame; else keyFrameCollection.RemoveAt(0);
Defensive patterns
Strategy: type-guard
Validate before calling
if (frame == null) keyFrames.RemoveAt(index); else keyFrames[index] = frame;
Type guard
static bool IsValidSlotAssignment(ColorKeyFrame frame) => frame != null;
Try / catch
try { keyFrames[i] = frame; }
catch (ArgumentNullException ex) when (ex.ParamName == $"ColorKeyFrameCollection[{i}]") { /* remove slot or supply default frame */ } Prevention
- Null-check key frames before indexer assignment
- Use RemoveAt instead of assigning null
- Validate data-bound collections for null items
When it happens
Trigger: Executing keyFrames[i] = null on a ColorKeyFrameCollection, or API/XAML paths that end up assigning a null key frame into an existing slot.
Common situations: Data-binding a null item into the collection; clearing logic that assigns null instead of removing; deserialization producing null entries.
Related errors
- DecimalKeyFrameCollection
- Animation_ChildMustBeKeyFrame
- DoubleKeyFrameCollection
- [[instance.TypeName]]KeyFrameCollection
- Int16KeyFrameCollection
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/3dcefe69a932d486.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/ColorKeyFrameCollection.cs:466
}
}
/// <summary>
/// Gets or sets the ColorKeyFrame at a given index.
/// </summary>
public ColorKeyFrame this[int index]
{
get
{
ReadPreamble();
return _keyFrames[index];
}
set
{
if (value == null)
{
throw new ArgumentNullException(String.Format(CultureInfo.InvariantCulture, "ColorKeyFrameCollection[{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)