dotnet/wpf · error · ArgumentNullException
BooleanKeyFrameCollection
Error message
BooleanKeyFrameCollection[{index}] What it means
The indexer setter of BooleanKeyFrameCollection throws ArgumentNullException when a caller assigns null into the collection at a given index. The exception's message is built as "BooleanKeyFrameCollection[{index}]" and passed as the paramName-style argument, identifying the position that received null. Key frame collections require a concrete BooleanKeyFrame instance at every slot.
Solutions
- Assign a valid non-null BooleanKeyFrame instance (e.g. new DiscreteBooleanKeyFrame(true, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)))) instead of null
- Check the value for null before inserting and skip or substitute a default key frame
- If coming from XAML binding, verify the bound resource/key actually resolves to a BooleanKeyFrame
Example fix
// before frames[0] = GetKeyFrame(); // may return null // after var kf = GetKeyFrame(); frames[0] = kf ?? new DiscreteBooleanKeyFrame(false, KeyTime.FromTimeSpan(TimeSpan.Zero));
Defensive patterns
Strategy: validation
Validate before calling
if (frame == null) throw new ArgumentException("Key frame cannot be null", nameof(frame));
booleanKeyFrameCollection[index] = frame; Type guard
static bool IsValidKeyFrame(BooleanKeyFrame f) => f != null;
Try / catch
try { collection[index] = frame; }
catch (ArgumentNullException ex) { /* ex.ParamName contains "BooleanKeyFrameCollection[i]"; supply a valid key frame or skip */ } Prevention
- Never assign null into key-frame collections; use the typed Add methods
- Null-check key frames produced by factories or resource lookups before insertion
- Prefer building key frames with object initializers so nulls fail fast
When it happens
Trigger: Assigning null via the indexer: booleanKeyFrameCollection[i] = null; or any data-bound/generated code path that writes a null key frame into the collection.
Common situations: XAML or code that builds a key-frame animation where a resource fails to resolve to a BooleanKeyFrame, or computed values that yield null before being inserted.
Related errors
- ByteKeyFrameCollection
- CharKeyFrameCollection
- anchorLocator.Parts
- Animation_AnimationTimelineTypeMismatch
- Animation_CalculatedValueIsInvalidForProperty
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/51c64fac075ae38d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/Generated/BooleanKeyFrameCollection.cs:466
}
}
/// <summary>
/// Gets or sets the BooleanKeyFrame at a given index.
/// </summary>
public BooleanKeyFrame this[int index]
{
get
{
ReadPreamble();
return _keyFrames[index];
}
set
{
if (value == null)
{
throw new ArgumentNullException(String.Format(CultureInfo.InvariantCulture, "BooleanKeyFrameCollection[{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)