dotnet/wpf · error · ArgumentNullException
CharKeyFrameCollection
Error message
CharKeyFrameCollection[{0}] What it means
The indexer setter of CharKeyFrameCollection throws ArgumentNullException (message "CharKeyFrameCollection[{0}]") when null is assigned into the collection at an index. Every slot must hold a concrete CharKeyFrame instance.
Solutions
- Assign a valid CharKeyFrame (e.g. new DiscreteCharKeyFrame('a', KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)))) instead of null
- Null-check before inserting and skip or substitute a default key frame
- Verify XAML resource keys resolve to CharKeyFrame instances
Example fix
// before
frames[0] = GetCharKeyFrame(); // may return null
// after
var kf = GetCharKeyFrame();
frames[0] = kf ?? new DiscreteCharKeyFrame('\0', KeyTime.FromTimeSpan(TimeSpan.Zero)); Defensive patterns
Strategy: validation
Validate before calling
if (frame == null) throw new ArgumentException("Key frame cannot be null", nameof(frame));
charKeyFrameCollection[index] = frame; Type guard
static bool IsValidKeyFrame(CharKeyFrame f) => f != null;
Try / catch
try { collection[index] = frame; }
catch (ArgumentNullException ex) { /* message names the index; supply a valid CharKeyFrame */ } Prevention
- Never assign null into key-frame collections; use typed Add methods
- Null-check key frames from factories/resources before insertion
- Use collection initializers to catch nulls at authoring time
When it happens
Trigger: charKeyFrameCollection[i] = null; or any generated/binding code path inserting a null CharKeyFrame into the collection.
Common situations: Unresolved key-frame resources in XAML, or dynamically computed key frames that can evaluate to null.
Related errors
- BooleanKeyFrameCollection
- ByteKeyFrameCollection
- anchorLocator.Parts
- Animation_AnimationTimelineTypeMismatch
- Animation_CalculatedValueIsInvalidForProperty
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/71d3b259f927c61e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/CharKeyFrameCollection.cs:466
}
}
/// <summary>
/// Gets or sets the CharKeyFrame at a given index.
/// </summary>
public CharKeyFrame this[int index]
{
get
{
ReadPreamble();
return _keyFrames[index];
}
set
{
if (value == null)
{
throw new ArgumentNullException(String.Format(CultureInfo.InvariantCulture, "CharKeyFrameCollection[{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)