{"record":{"id":"392463952281fcb0","repo":"dotnet/wpf","slug":"new-argumentnullexception-name","errorCode":null,"errorMessage":"new ArgumentNullException(name)","messagePattern":"new ArgumentNullException\\(name\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/Text/TextRange.cs","lineNumber":449,"sourceCode":"\n        #region Private Methods\n\n        // check an endpoint argument to see if it is valid.\n        private void ValidateEndpointArgument(TextPatternRangeEndpoint endpoint, string name)\n        {\n            if (endpoint != TextPatternRangeEndpoint.Start && endpoint != TextPatternRangeEndpoint.End)\n            {\n                Misc.ThrowInvalidArgument(name);\n            }\n        }\n\n        // check a range argument to see if it is valid.\n        private void ValidateRangeArgument(TextPatternRange range, string name)\n        {\n            // check if the argument is null\n            if (range == null)\n            {\n                throw new ArgumentNullException(name);\n            }\n\n            // check if the range comes from a different text pattern.\n            if (!TextPattern.Compare(_pattern, range._pattern))\n            {\n                Misc.ThrowInvalidArgument(name);\n            }\n}\n\n        // check an unit argument to see if it is valid.\n        private void ValidateUnitArgument(TextUnit unit, string name)\n        {\n            if (unit<TextUnit.Character || unit>TextUnit.Document)\n            {\n                Misc.ThrowInvalidArgument(name);\n            }\n        }\n","sourceCodeStart":431,"sourceCodeEnd":467,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/Text/TextRange.cs#L431-L467","documentation":"ValidateRangeArgument throws ArgumentNullException when a TextPatternRange parameter passed to Compare, CompareEndpoints, or MoveEndpointByRange is null. The methods require a second valid range obtained from the same TextPattern instance.","triggerScenarios":"Calling range.Compare(null), range.CompareEndpoints(TextPatternRangeEndpoint.Start, null, ...), or range.MoveEndpointByRange(..., null) — typically when the other range came from a failed lookup, a disconnected element, or an uninitialized variable.","commonSituations":"Ranges stored in collections where entries may be null; earlier provider calls returning null (element vanished); refactoring that forgot to initialize the target range.","solutions":["Null-check the TextPatternRange argument before invoking Compare/CompareEndpoints/MoveEndpointByRange.","Ensure the range was successfully created via TextPattern.DocumentRange/GetSelection/RangeFromChild and not null.","If the source range may be stale, re-acquire it from a live AutomationElement."],"exampleFix":"// before\nif (range.Compare(otherRange)) { ... } // otherRange may be null\n// after\nif (otherRange != null && range.Compare(otherRange)) { ... }","handlingStrategy":"validation","validationCode":"if (otherRange == null) throw new ArgumentNullException(nameof(otherRange)); // or early-return/throw in caller before Compare/CompareEndpoints/MoveEndpointByRange","typeGuard":"static bool IsUsableRange(TextPatternRange r) => r is not null;","tryCatchPattern":"try { return range.CompareEndpoints(TextPatternRangeEndpoint.Start, otherRange, TextPatternRangeEndpoint.Start); }\ncatch (ArgumentNullException) { return int.MinValue; // or rethrow with context }","preventionTips":["Null-check ranges stored in collections before comparing","Re-derive ranges from live elements rather than caching them across UI updates","Use pattern matching / null-conditional checks in helper methods that take TextPatternRange"],"tags":["uiautomation","textpattern","null-argument","argument-null-exception"],"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"}