{"record":{"id":"10238614a58e6a89","repo":"dotnet/wpf","slug":"sr-noelement","errorCode":null,"errorMessage":"SR.NoElement","messagePattern":"SR\\.NoElement","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextContainer.cs","lineNumber":3223,"sourceCode":"\n        #region ValidateXXXHelpers\n\n        // Validation for SetValue.\n        private void ValidateSetValue(TextPointer position)\n        {\n            TextElement element;\n\n            if (position.TextContainer != this)\n            {\n                throw new InvalidOperationException(SR.Format(SR.NotInThisTree, \"position\"));\n            }\n\n            position.SyncToTreeGeneration();\n\n            element = position.Parent as TextElement;\n            if (element == null)\n            {\n                throw new InvalidOperationException(SR.NoElement);\n            }\n        }\n\n        #endregion ValidateXXXHelpers\n\n        // Verifies the TextContainer symbol count is in synch with the TextTreeText\n        // character count.\n        private void AssertTreeAndTextSize()\n        {\n            if (Invariant.Strict)\n            {\n                int count;\n                TextTreeTextBlock textBlock;\n\n                if (_rootNode.RootTextBlock != null)\n                {\n                    count = 0;\n","sourceCodeStart":3205,"sourceCodeEnd":3241,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextContainer.cs#L3205-L3241","documentation":"TextContainer's validation helper requires the given TextPointer's parent to be a TextElement. When the parent is null or not a TextElement (e.g. the pointer is scoped to the root TextContainer itself), the container throws InvalidOperationException(SR.NoElement) because there is no containing element to operate on. This is an internal invariant guard for pointer scoping in the WPF text stack.","triggerScenarios":"Calling TextContainer APIs (e.g. validation helpers used by TextPointer/TextElement operations) with a TextPointer whose Parent is the TextContainer root or otherwise not a TextElement.","commonSituations":"Using stale TextPointer positions after tree edits/generations changed; obtaining positions at document scope (Paragraph-less containers) and passing them to element-scoped APIs; programmatic text manipulation in RichTextBox/TextBox scenarios.","solutions":["Ensure the TextPointer's parent is a TextElement before calling element-scoped APIs (e.g. check pointer.Parent as TextElement).","Re-position the pointer so it is inside a TextElement, or use container-scoped APIs instead.","Refresh stale pointers against the current tree generation after edits.","Wrap in try/catch for InvalidOperationException when working with volatile pointer positions."],"exampleFix":"// before\nvar element = (TextElement)textPointer.Parent;\n// after\nvar element = textPointer.Parent as TextElement;\nif (element == null) throw new ArgumentException(\"Pointer is not inside a TextElement\");","handlingStrategy":"type-guard","validationCode":"bool canUse = textPointer.Parent is TextElement;\nif (!canUse) throw new InvalidOperationException(\"Pointer must be inside a TextElement\");","typeGuard":"TextElement elt = textPointer?.Parent as TextElement;\nbool isValid = elt != null;","tryCatchPattern":"try { containerApi(pointer); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"NoElement\") || ex.Message.StartsWith(\"Text element\")) { /* fallback: re-acquire pointer inside a TextElement */ }","preventionTips":["Always check TextPointer.Parent before element-scoped operations","Re-validate pointers after tree mutations","Avoid holding pointers across multiple edit operations"],"tags":["wpf","textpointer","invalid-operation"],"backgroundTag":"invalid-state-transition","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"}