{"record":{"id":"83a6e1db17980743","repo":"dotnet/wpf","slug":"argumentnullexception-paramname","errorCode":null,"errorMessage":"ArgumentNullException(paramName)","messagePattern":"ArgumentNullException\\(paramName\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs","lineNumber":39,"sourceCode":"        #region Internal Methods\n\n        // Verifies a TextPointer is non-null and\n        // is associated with a given TextContainer.\n        //\n        // Throws an appropriate exception if a test fails.\n        internal static void VerifyPosition(ITextContainer tree, ITextPointer position)\n        {\n            VerifyPosition(tree, position, nameof(position));\n        }\n        \n        // Verifies a TextPointer is non-null and is associated with a given TextContainer.\n        //\n        // Throws an appropriate exception if a test fails.\n        internal static void VerifyPosition(ITextContainer container, ITextPointer position, string paramName)\n        {\n            if (position == null)\n            {\n                throw new ArgumentNullException(paramName);\n            }\n\n            if (position.TextContainer != container)\n            {\n                throw new ArgumentException(SR.Format(SR.NotInAssociatedTree, paramName));\n            }\n        }\n\n        // Verifies two positions are safe to use as a logical text span.\n        //\n        // Throws ArgumentNullException if startPosition == null || endPosition == null\n        //        ArgumentException if startPosition.TextContainer != endPosition.TextContainer or\n        //                             startPosition > endPosition\n        internal static void VerifyPositionPair(ITextPointer startPosition, ITextPointer endPosition)\n        {\n            ArgumentNullException.ThrowIfNull(startPosition);\n            ArgumentNullException.ThrowIfNull(endPosition);\n            if (startPosition.TextContainer != endPosition.TextContainer)","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs#L21-L57","documentation":"ValidationHelper.VerifyPosition throws ArgumentNullException(paramName) when a position argument passed alongside an ITextContainer is null. This guard exists so text APIs fail fast with the correct parameter name instead of NullReferenceException deep in text-tree code.","triggerScenarios":"Calling any ITextContainer/TextEditor API that validates a position (e.g. TextContainer.InsertText(position,...), selection APIs) with position == null.","commonSituations":"CaretPosition/Selection captured before the control was initialized; storing a TextPointer after the document was replaced (it becomes invalid/null); async code reading TextEditor.Selection.Start before load.","solutions":["Null-check TextPointer properties (CaretPosition, Selection.Start/End) before use and re-query after document load.","Wait for Loaded/TextChanged before capturing positions.","Catch ArgumentNullException and treat the pointer as invalid — re-acquire from the container."],"exampleFix":"// before\ncontainer.InsertText(cachedPosition, text); // cachedPosition may be null\n// after\nvar pos = cachedPosition ?? textBox.CaretPosition;\nif (pos != null && pos.TextContainer == container) container.InsertText(pos, text);","handlingStrategy":"type-guard","validationCode":"if (position == null) { reAcquireFromContainer(); return; }","typeGuard":"bool IsValidPointer(ITextContainer c, ITextPointer p) => p != null && p.TextContainer == c;","tryCatchPattern":"try { UsePosition(pos); }\ncatch (ArgumentNullException) { pos = control.CaretPosition; UsePosition(pos); }","preventionTips":["Null-check CaretPosition/Selection.Start before use","Capture pointers only after Loaded","Re-query pointers instead of caching stale ones"],"tags":["wpf","argumentnull","textpointer"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}