{"record":{"id":"41cdbf5d117236e9","repo":"dotnet/wpf","slug":"annotation-component","errorCode":null,"errorMessage":"annotation component","messagePattern":"annotation component","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/AdornerPresentationContext.cs","lineNumber":44,"sourceCode":"    internal sealed class AdornerPresentationContext : PresentationContext\n    {\n        #region Constructors\n\n        /// <summary>\n        /// Create an initialized instance of an AdornerPresentationContext. Set the presentation context of the \n        /// component wrapped in the adorner\n        /// </summary>\n        /// <param name=\"adornerLayer\">AdornerLayer this presentation context is on, must not be null</param>\n        /// <param name=\"adorner\">AnnotationAdorner that wraps the annotation component.  Will be null in case of creating enclosing context</param>\n        private AdornerPresentationContext(AdornerLayer adornerLayer, AnnotationAdorner adorner)\n        {\n            ArgumentNullException.ThrowIfNull(adornerLayer);\n\n            _adornerLayer = adornerLayer;\n            if (adorner != null)\n            {\n                if (adorner.AnnotationComponent == null)\n                    throw new ArgumentNullException(\"annotation component\");\n                if (adorner.AnnotationComponent.PresentationContext != null)\n                    throw new InvalidOperationException(SR.Format(SR.ComponentAlreadyInPresentationContext, adorner.AnnotationComponent));\n                _annotationAdorner = adorner;\n            }\n        }\n\n        #endregion Constructors\n\n        #region Static Methods\n\n        /// <summary>\n        /// Host a component in an adorner layer.\n        /// Wrap the component in an annotation adorner, add that to the adorner layer, create and set presentation context and invalidate to pick up styles.\n        /// Note, this is called from two places: (1) component manager to host choosen annotation component, and (2) presentation context when component\n        /// adds additional IAnnotationComponent.\n        /// </summary>\n        /// <param name=\"adornerLayer\">Adorner layer the component is hosted in</param>\n        /// <param name=\"component\">Component that is being hosted</param>","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/AdornerPresentationContext.cs#L26-L62","documentation":"The AdornerPresentationContext constructor throws ArgumentNullException with name \"annotation component\" when a non-null AnnotationAdorner is supplied but its AnnotationComponent property is null. An adorner presented in an annotation context must carry its AnnotationComponent; a subsequent InvalidOperationException guards components already in another presentation context.","triggerScenarios":"Constructing AdornerPresentationContext(adornerLayer, adorner) where adorner.AnnotationComponent was never set or was cleared; creating a bare AnnotationAdorner without associating a component.","commonSituations":"Custom adorner subclasses forgetting to initialize AnnotationComponent; component lifetime teardown clearing the reference before context creation; wiring adorners manually instead of through AnnotationService.","solutions":["Set the adorner's AnnotationComponent before constructing the AdornerPresentationContext","Construct the AnnotationComponent and pass it to the AnnotationAdorner constructor","If the component is intentionally absent, pass null for the adorner parameter instead"],"exampleFix":"// before\nvar adorner = new AnnotationAdorner(); // AnnotationComponent never set\nnew AdornerPresentationContext(layer, adorner);\n// after\nvar adorner = new AnnotationAdorner(myAnnotationComponent);\nnew AdornerPresentationContext(layer, adorner);","handlingStrategy":"validation","validationCode":"if (adorner != null && adorner.AnnotationComponent == null)\n    throw new InvalidOperationException(\"Adorner must have an AnnotationComponent before creating AdornerPresentationContext.\");","typeGuard":"bool HasAnnotationComponent(AnnotationAdorner adorner) => adorner?.AnnotationComponent != null;","tryCatchPattern":"try { var ctx = new AdornerPresentationContext(layer, adorner); }\ncatch (ArgumentNullException) { /* initialize AnnotationComponent and retry */ }\ncatch (InvalidOperationException) { /* component already presented elsewhere — detach first */ }","preventionTips":["Always construct AnnotationAdorner with its AnnotationComponent set","Detach a component from its previous presentation context before reuse","Route adorner creation through AnnotationService rather than manual wiring"],"tags":["wpf","annotations","null-argument","adorner"],"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"}