dotnet/wpf · error · ArgumentNullException

visual

Error message

visual

What it means

PrintContext.Render renders a Visual to the printer and requires a non-null visual; passing null triggers ArgumentNullException("visual"). This guards the print pipeline from dereferencing a missing visual tree.

Solutions

  1. Ensure the Visual reference is non-null before calling Print/Render (check Loaded event completed)
  2. Resolve the element by name after InitializeComponent and null-check the result
  3. If printing a dynamically created visual, construct it (e.g. new FixedPage()) rather than passing an uninitialized variable
  4. Wrap in a guard that throws a clearer app-level message when the visual cannot be resolved

Example fix

// before
printContext.Print(null);
// after
var visual = this.FindName("ContentToPrint") as Visual;
if (visual == null) throw new InvalidOperationException("ContentToPrint was not found");
printContext.Print(visual);
Defensive patterns

Strategy: type-guard

Validate before calling

if (visual == null) throw new InvalidOperationException("Visual to print was not resolved");

Type guard

bool IsPrintable(Visual v) => v != null && v.IsAncestorOf(v) || v != null;

Try / catch

try { printContext.Print(visual); }
catch (ArgumentNullException ex) when (ex.ParamName == "visual") { log.Error("Visual was null at print time", ex); }

Prevention

When it happens

Trigger: Calling PrintContext.Print (which calls Render) with a Visual variable that is null — e.g. a control not yet loaded, a FindResource/FindName lookup that returned null, or a variable assigned conditionally.

Common situations: Printing before the Window/Page is loaded so the element reference is null; DataTemplate element lookup failing; passing the result of VisualTreeHelper.GetChild out of range; null from a failed x:Name resolution.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/printcontext.cs:506

            }
        }

        static private void AssertPositive(int val, string name)
        {
            if (val <= 0)
            {
                throw new ArgumentOutOfRangeException(name, SR.ParameterMustBeGreaterThanZero);
            }
        }

        /// <summary>
        /// Render visual to printer.
        /// </summary>
        private void Render(Visual visual, string uri)
        {
            if (visual == null)
            {
                throw new ArgumentNullException("visual");
            }

            int dpiX  = _jobTicket.PageResolution.ResolutionX;
            int dpiY  = _jobTicket.PageResolution.ResolutionY;

            if (dpiX < 0) { dpiX = 600; }

            if (dpiY < 0) { dpiY = 600; }

            AssertPositive(dpiX, "PageResolution.ResolutionX");
            AssertPositive(dpiY, "PageResolution.ResolutionY");

            int sizeX = (int) (_jobTicket.PageMediaSize.MediaSizeX * dpiX);
            int sizeY = (int) (_jobTicket.PageMediaSize.MediaSizeY * dpiY);

            AssertPositive(sizeX, "PageCanvasSizeCap.CanvasSizeX");
            AssertPositive(sizeY, "PageCanvasSizeCap.CanvasSizeY");

View on GitHub (pinned to 81131a70a4)