{"record":{"id":"6c189761eb42eba7","repo":"dotnet/wpf","slug":"anchorlocator-parts","errorCode":null,"errorMessage":"anchorLocator.Parts","messagePattern":"anchorLocator\\.Parts","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Storage/XmlStreamStore.cs","lineNumber":232,"sourceCode":"        ///     that contains a locator that begins with the locator parts\n        ///     in anchorLocator.\n        /// </summary>\n        /// <param name=\"anchorLocator\">the locator we are looking for</param>\n        /// <returns>\n        ///    A list of annotations that have locators in their anchors\n        ///    starting with the same locator parts list as of the input locator\n        ///    If no such annotations an empty list will be returned. The method\n        ///    never returns null.\n        /// </returns>\n        /// <exception cref=\"ObjectDisposedException\">if object has been Disposed</exception>\n        /// <exception cref=\"InvalidOperationException\">the stream is null</exception>\n        public override IList<Annotation> GetAnnotations(ContentLocator anchorLocator)\n        {\n            // First we generate the XPath expression\n            ArgumentNullException.ThrowIfNull(anchorLocator);\n\n            if (anchorLocator.Parts == null)\n                throw new ArgumentNullException(\"anchorLocator.Parts\");\n\n            //fire trace event\n            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.GetAnnotationByLocBegin);\n            IList<Annotation> annotations = null;\n            try\n            {\n                string query = $@\"//{AnnotationXmlConstants.Prefixes.CoreSchemaPrefix}:{AnnotationXmlConstants.Elements.ContentLocator}\";\n\n                if (anchorLocator.Parts.Count > 0)\n                {\n                    query += @\"/child::*[1]/self::\";\n                    for (int i = 0; i < anchorLocator.Parts.Count; i++)\n                    {\n                        if (anchorLocator.Parts[i] != null)\n                        {\n                            if (i > 0)\n                            {\n                                query += @\"/following-sibling::\";","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Storage/XmlStreamStore.cs#L214-L250","documentation":"GetAnnotations(ContentLocator anchorLocator) throws ArgumentNullException with the literal parameter name \"anchorLocator.Parts\" when the locator is non-null but its Parts collection is null. Locating annotations by anchor requires a non-null Parts list to build the XPath expression.","triggerScenarios":"Calling XmlStreamStore.GetAnnotations(locator) where locator is a valid ContentLocator instance but locator.Parts was never initialized (null).","commonSituations":"Deserializing a ContentLocator where the Parts element was absent; constructing 'new ContentLocator()' without calling Parts.Add or assigning a parts collection; passing a default/uninitialized locator.","solutions":["Initialize the Parts collection before calling: locator.Parts = new List<ContentLocatorPart>(); then add at least one ContentLocatorPart.","Pass 'null' instead of a hollow locator if you intend to fetch all annotations (GetAnnotations() overload without locator).","Guard the call: if (locator?.Parts == null) use the parameterless GetAnnotations()."],"exampleFix":"// before\nvar locator = new ContentLocator();\nvar results = store.GetAnnotations(locator);\n\n// after\nvar locator = new ContentLocator();\nlocator.Parts.Add(new ContentLocatorPart(new XmlQualifiedName(\"Page\", \"...\")));\nvar results = store.GetAnnotations(locator);","handlingStrategy":"type-guard","validationCode":"if (locator == null || locator.Parts == null) { /* use parameterless GetAnnotations() or initialize Parts */ }","typeGuard":"bool HasParts(ContentLocator locator) => locator?.Parts != null && locator.Parts.Count > 0;","tryCatchPattern":"try { return store.GetAnnotations(locator); }\ncatch (ArgumentNullException) { return store.GetAnnotations(); }","preventionTips":["Never construct a ContentLocator without adding at least one ContentLocatorPart.","Prefer the parameterless GetAnnotations() when you do not intend to filter by anchor.","Centralize locator construction in a factory that guarantees Parts initialization."],"tags":["null-reference","wpf","annotations","argument-null"],"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"}