{"record":{"id":"67fc192f99f2aba1","repo":"dotnet/wpf","slug":"sr-drawinggroup-cannotappendtonullcollection","errorCode":null,"errorMessage":"SR.DrawingGroup_CannotAppendToNullCollection","messagePattern":"SR\\.DrawingGroup_CannotAppendToNullCollection","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/DrawingGroup.cs","lineNumber":117,"sourceCode":"                // of the reference here to avoid any more unneccesary copies.\n                Children = rootDrawingGroupChildren;\n            }\n            else                \n            {\n                //\n                //\n                // Append the collection to the current Children collection                \n                //\n                //\n                DrawingCollection children = Children;\n\n                // \n                // Ensure that we can Append to the Children collection\n                //\n                \n                if (children == null)\n                {\n                    throw new InvalidOperationException(SR.DrawingGroup_CannotAppendToNullCollection);                                \n                }\n               \n                if (children.IsFrozen)\n                {\n                    throw new InvalidOperationException(SR.DrawingGroup_CannotAppendToFrozenCollection);                                                  \n                }\n\n                // Append the new collection to our current Children.\n                //\n                // TransactionalAppend rolls-back the Append operation in the event\n                // an exception is thrown from the Changed event.                \n                children.TransactionalAppend(rootDrawingGroupChildren);\n            }            \n\n            // This DrawingGroup is no longer open\n            _open = false;\n        }\n","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/DrawingGroup.cs#L99-L135","documentation":"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.","triggerScenarios":"Calling DrawingGroup.Close(children) (or the internal close path) with children == null instead of an initialized DrawingCollection.","commonSituations":"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.","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"],"exampleFix":"// before\nDrawingCollection kids = GetChildrenMaybeNull();\ngroup.Close(kids); // throws when null\n\n// after\nDrawingCollection kids = GetChildrenMaybeNull() ?? new DrawingCollection();\ngroup.Close(kids);","handlingStrategy":"validation","validationCode":"if (children == null)\n    throw new ArgumentException(\"children must be a non-null DrawingCollection\", nameof(children));\ngroup.Close(children);","typeGuard":null,"tryCatchPattern":"try { group.Close(children); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Append\"))\n{\n    // recover: recreate the group with a fresh collection\n}","preventionTips":["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"],"tags":["wpf","drawinggroup","null-reference","collection"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}