dotnet/wpf · error · ArgumentNullException
ArgumentNullException(paramName)
Error message
ArgumentNullException(paramName)
What it means
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.
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.
Example fix
// before container.InsertText(cachedPosition, text); // cachedPosition may be null // after var pos = cachedPosition ?? textBox.CaretPosition; if (pos != null && pos.TextContainer == container) container.InsertText(pos, text);
Defensive patterns
Strategy: type-guard
Validate before calling
if (position == null) { reAcquireFromContainer(); return; } Type guard
bool IsValidPointer(ITextContainer c, ITextPointer p) => p != null && p.TextContainer == c;
Try / catch
try { UsePosition(pos); }
catch (ArgumentNullException) { pos = control.CaretPosition; UsePosition(pos); } Prevention
- Null-check CaretPosition/Selection.Start before use
- Capture pointers only after Loaded
- Re-query pointers instead of caching stale ones
When it happens
Trigger: Calling any ITextContainer/TextEditor API that validates a position (e.g. TextContainer.InsertText(position,...), selection APIs) with position == null.
Common situations: 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.
Related errors
- ArgumentNullException: child
- ArgumentNullException: hitTestParameters
- ArgumentNullException(nameof(scriptName))
- ArgumentNullException(nameof(text))
- block
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/83a6e1db17980743.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs:39
#region Internal Methods
// Verifies a TextPointer is non-null and
// is associated with a given TextContainer.
//
// Throws an appropriate exception if a test fails.
internal static void VerifyPosition(ITextContainer tree, ITextPointer position)
{
VerifyPosition(tree, position, nameof(position));
}
// Verifies a TextPointer is non-null and is associated with a given TextContainer.
//
// Throws an appropriate exception if a test fails.
internal static void VerifyPosition(ITextContainer container, ITextPointer position, string paramName)
{
if (position == null)
{
throw new ArgumentNullException(paramName);
}
if (position.TextContainer != container)
{
throw new ArgumentException(SR.Format(SR.NotInAssociatedTree, paramName));
}
}
// Verifies two positions are safe to use as a logical text span.
//
// Throws ArgumentNullException if startPosition == null || endPosition == null
// ArgumentException if startPosition.TextContainer != endPosition.TextContainer or
// startPosition > endPosition
internal static void VerifyPositionPair(ITextPointer startPosition, ITextPointer endPosition)
{
ArgumentNullException.ThrowIfNull(startPosition);
ArgumentNullException.ThrowIfNull(endPosition);
if (startPosition.TextContainer != endPosition.TextContainer)View on GitHub (pinned to 81131a70a4)