dotnet/wpf · error · InvalidOperationException
SR.DrawingGroup_CannotAppendToNullCollection
Error message
SR.DrawingGroup_CannotAppendToNullCollection
What it means
DrawingGroup.Close() was handed a null DrawingCollection to append into. Close builds the group's Children by appending the collected drawings, and appending to a null collection is not allowed, so it throws InvalidOperationException with SR.DrawingGroup_CannotAppendToNullCollection.
Solutions
- Pass a non-null DrawingCollection instance to Close (e.g. new DrawingCollection())
- Initialize the children collection before the using/Open block
- Null-check the collection before calling Close and log/repair if null
- Use the parameterless Open()/Append()/Close() pattern so the group manages Children itself
Example fix
// before DrawingCollection kids = GetChildrenMaybeNull(); group.Close(kids); // throws when null // after DrawingCollection kids = GetChildrenMaybeNull() ?? new DrawingCollection(); group.Close(kids);
Defensive patterns
Strategy: validation
Validate before calling
if (children == null)
throw new ArgumentException("children must be a non-null DrawingCollection", nameof(children));
group.Close(children); Try / catch
try { group.Close(children); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Append"))
{
// recover: recreate the group with a fresh collection
} Prevention
- Always initialize DrawingCollection fields with new DrawingCollection()
- Null-check collections at API boundaries before closing groups
- Prefer Open()/Append()/Close() over passing collections to Close
When it happens
Trigger: Calling DrawingGroup.Close(children) (or the internal close path) with children == null instead of an initialized DrawingCollection.
Common situations: Manually calling Close with a field that was never initialized; a factory/method that can return null being passed straight to Close; refactoring that removed the 'new DrawingCollection()' initialization.
Related errors
- anchorLocator.Parts
- Cannot pass multidimensional array to the CopyTo method on…
- DoubleKeyFrameCollection
- dpiInfo cannot be null
- Int16KeyFrameCollection
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/67fc192f99f2aba1.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/DrawingGroup.cs:117
// of the reference here to avoid any more unneccesary copies.
Children = rootDrawingGroupChildren;
}
else
{
//
//
// Append the collection to the current Children collection
//
//
DrawingCollection children = Children;
//
// Ensure that we can Append to the Children collection
//
if (children == null)
{
throw new InvalidOperationException(SR.DrawingGroup_CannotAppendToNullCollection);
}
if (children.IsFrozen)
{
throw new InvalidOperationException(SR.DrawingGroup_CannotAppendToFrozenCollection);
}
// Append the new collection to our current Children.
//
// TransactionalAppend rolls-back the Append operation in the event
// an exception is thrown from the Changed event.
children.TransactionalAppend(rootDrawingGroupChildren);
}
// This DrawingGroup is no longer open
_open = false;
}
View on GitHub (pinned to 81131a70a4)