dotnet/wpf · error · ArgumentException

SR.AnnotationIsNull

Error message

SR.AnnotationIsNull

What it means

HighlightComponent.CheckInputData throws ArgumentException(SR.AnnotationIsNull) when the attached annotation wrapper carries a null Annotation object even though the anchor itself was a valid TextAnchor. The component needs the actual Annotation instance to compare its AnnotationType and read its resource payload (colors), so a null annotation cannot be processed.

Solutions

  1. Ensure the IAttachedAnnotation passed to AddAttachedAnnotation has a non-null Annotation property.
  2. If loading annotations from a store, verify the load succeeded (Annotation not null) before attaching to the component.
  3. Fix custom IAttachedAnnotation implementations to always set Annotation in the constructor.

Example fix

// before
attachedAnnotation = new AttachedAnnotation(anchor, null, level);
// after
if (annotation == null) throw new ArgumentNullException(nameof(annotation));
attachedAnnotation = new AttachedAnnotation(anchor, annotation, level);
Defensive patterns

Strategy: validation

Validate before calling

if (attachedAnnotation?.Annotation == null) return; // do not attach

Type guard

static bool HasAnnotation(IAttachedAnnotation a) => a?.Annotation != null;

Try / catch

try { component.AddAttachedAnnotation(attachedAnnotation); } catch (ArgumentException ex) when (ex.ParamName == nameof(attachedAnnotation)) { log.Warn("null annotation"); }

Prevention

When it happens

Trigger: AddAttachedAnnotation invoked with an IAttachedAnnotation implementation whose Annotation property returns null; hand-rolled IAttachedAnnotation implementations that forget to set Annotation.

Common situations: Custom annotation loaders that populate AttachedAnchor but not Annotation; deserialization failures that silently leave the Annotation null; unit-test stubs missing property setup.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/01f176ce32913281. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs:522

        /// <returns>The AttachedAnchor TextContainer</returns>
        private ITextContainer CheckInputData(IAttachedAnnotation attachedAnnotation)
        {
            ArgumentNullException.ThrowIfNull(attachedAnnotation);

            TextAnchor textAnchor = attachedAnnotation.AttachedAnchor as TextAnchor;
            if (textAnchor == null)
            {
                throw new ArgumentException(SR.InvalidAttachedAnchor, nameof(attachedAnnotation));
            }

            //this should be in a fixed or flow textcontainer
            ITextContainer textContainer = textAnchor.Start.TextContainer;

            Invariant.Assert(textContainer != null, "TextAnchor does not belong to a TextContainer");

            if (attachedAnnotation.Annotation == null)
            {
                throw new ArgumentException(SR.AnnotationIsNull, nameof(attachedAnnotation));
            }

            //check annotation type
            if (!_type.Equals(attachedAnnotation.Annotation.AnnotationType))
            {
                throw new ArgumentException(SR.Format(SR.NotHighlightAnnotationType, attachedAnnotation.Annotation.AnnotationType.ToString()), nameof(attachedAnnotation));
            }

            return textContainer;
        }

        /// <summary>
        /// Converts a string to Color object
        /// </summary>
        /// <param name="color">Color string</param>
        /// <returns>Color object</returns>
        private static Color GetColor(string color)
        {

View on GitHub (pinned to 81131a70a4)