dotnet/wpf · error · ArgumentNullException

ByteKeyFrameCollection

Error message

ByteKeyFrameCollection[{index}]

What it means

The indexer setter of ByteKeyFrameCollection throws ArgumentNullException (message "ByteKeyFrameCollection[{index}]") when null is assigned into the collection at an index. Every slot in the collection must hold a concrete ByteKeyFrame instance.

Solutions

  1. Assign a valid ByteKeyFrame (e.g. new LinearByteKeyFrame((byte)10, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)))) instead of null
  2. Null-check before inserting and skip or substitute a default key frame
  3. Verify XAML resources for key frames resolve to the correct type

Example fix

// before
frames[0] = MakeKeyFrame(); // may return null
// after
var kf = MakeKeyFrame();
frames[0] = kf ?? new DiscreteByteKeyFrame(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));
byteKeyFrameCollection[index] = frame;

Type guard

static bool IsValidKeyFrame(ByteKeyFrame f) => f != null;

Try / catch

try { collection[index] = frame; }
catch (ArgumentNullException ex) { /* message names the index; supply a valid ByteKeyFrame */ }

Prevention

When it happens

Trigger: Assigning null via the indexer: byteKeyFrameCollection[i] = null; or a binding/generated code path that writes a null ByteKeyFrame into the collection.

Common situations: Failed resource resolution for a key frame in XAML, or code that computes key frames dynamically and can produce null entries.

Related errors


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

Appendix: source

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

            }
        }

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

                return _keyFrames[index];
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException(String.Format(CultureInfo.InvariantCulture, "ByteKeyFrameCollection[{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)