dotnet/wpf · error · ArgumentNullException
RectKeyFrameCollection
Error message
RectKeyFrameCollection[{0}] What it means
RectKeyFrameCollection's this[int] setter throws ArgumentNullException whose (misused) message parameter is the formatted string "RectKeyFrameCollection[{index}]" when a null RectKeyFrame is assigned at the given index. The collection forbids null key frames; the formatted string is used as the paramName, identifying the failing index.
Solutions
- Check for null before assigning: only set collection[index] when the key frame is non-null
- Create a valid RectKeyFrame instance instead of assigning null
- Use Add() and validate elements before insertion
Example fix
// before keyFrames[0] = GetKeyFrame(); // may return null // after var kf = GetKeyFrame(); if (kf != null) keyFrames[0] = kf;
Defensive patterns
Strategy: type-guard
Validate before calling
if (kf == null) throw new InvalidOperationException("Cannot assign null key frame at index " + index); keyFrames[index] = kf; Type guard
bool IsValidKeyFrame(RectKeyFrame kf) => kf != null;
Try / catch
try { keyFrames[index] = kf; } catch (ArgumentNullException ex) { /* kf was null at ex.ParamName index */ } Prevention
- Null-check key frames before indexed assignment
- Ensure factory methods never return null key frames
- Prefer Add() with validation over direct index sets
When it happens
Trigger: Executing collection[index] = null on a RectKeyFrameCollection, e.g. myCollection[0] = null;
Common situations: Programmatic collection manipulation where a key frame variable is null (failed lookup/creation) before assignment; generic collection-filling code that doesn't check for null elements.
Related errors
- Int32KeyFrameCollection
- Int64KeyFrameCollection
- PointKeyFrameCollection
- ColorKeyFrameCollection
- DecimalKeyFrameCollection
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/5a5a50d0b2f52f9e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/RectKeyFrameCollection.cs:466
}
}
/// <summary>
/// Gets or sets the RectKeyFrame at a given index.
/// </summary>
public RectKeyFrame this[int index]
{
get
{
ReadPreamble();
return _keyFrames[index];
}
set
{
if (value == null)
{
throw new ArgumentNullException(String.Format(CultureInfo.InvariantCulture, "RectKeyFrameCollection[{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)