dotnet/wpf · error · ArgumentNullException

[[instance.TypeName]]KeyFrameCollection

Error message

[[instance.TypeName]]KeyFrameCollection[{0}]

What it means

Thrown by a KeyFrameCollectionTemplate-generated KeyFrameCollection indexer setter when value is null — the collection indexers do not permit null key frames. This is a codegen template emitting an ArgumentNullException naming the index position.

Solutions

  1. Ensure the key frame instance is constructed before assigning: collection[i] = new LinearDoubleKeyFrame(...).
  2. Remove the index instead of setting null: use RemoveAt(i) to delete an entry.
  3. Guard loops/serialization that may produce null frames before touching the collection.

Example fix

// before
doubleKeyFrames[0] = null; // trying to clear the frame
// after
doubleKeyFrames.RemoveAt(0);
Defensive patterns

Strategy: validation

Validate before calling

if (frame == null) throw new InvalidOperationException($"Key frame at index {i} is null; construct it before assignment.");
collection[i] = frame;

Type guard

static bool IsValidFrame<T>(T frame) where T : class => frame != null;

Try / catch

try { collection[i] = frame; } catch (ArgumentNullException ex) when (ex.ParamName == $"{i}" || ex.Message.Contains($"[{i}]")) { log.Warn($"Skipping null key frame at index {i}"); }

Prevention

When it happens

Trigger: Assigning null into a key-frame collection via the indexer, e.g. collection[0] = null, or databinding/serialization writing null into the list.

Common situations: Programmatic key-frame animation construction (DoubleKeyFrameCollection, ColorKeyFrameCollection) with an uninitialized frame variable; XAML or tooling emitting a null child; deserialization of incomplete animation markup.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/KeyFrameCollectionTemplate.cs:526

                                        }
                                    }
                                    
                                    /// <summary>
                                    /// Gets or sets the [[instance.TypeName]]KeyFrame at a given index.
                                    /// </summary>
                                    public [[instance.TypeName]]KeyFrame this[int index]
                                    {
                                        get
                                        {
                                            ReadPreamble();
                                            
                                            return _keyFrames[index];
                                        }
                                        set
                                        {
                                            if (value == null)
                                            {
                                                throw new ArgumentNullException(String.Format(CultureInfo.InvariantCulture, "[[instance.TypeName]]KeyFrameCollection[{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)