{"record":{"id":"b13faa0e8173f9a2","repo":"dotnet/wpf","slug":"visual","errorCode":null,"errorMessage":"visual","messagePattern":"visual","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/printcontext.cs","lineNumber":506,"sourceCode":"            }\n        }\n\n        static private void AssertPositive(int val, string name)\n        {\n            if (val <= 0)\n            {\n                throw new ArgumentOutOfRangeException(name, SR.ParameterMustBeGreaterThanZero);\n            }\n        }\n\n        /// <summary>\n        /// Render visual to printer.\n        /// </summary>\n        private void Render(Visual visual, string uri)\n        {\n            if (visual == null)\n            {\n                throw new ArgumentNullException(\"visual\");\n            }\n\n            int dpiX  = _jobTicket.PageResolution.ResolutionX;\n            int dpiY  = _jobTicket.PageResolution.ResolutionY;\n\n            if (dpiX < 0) { dpiX = 600; }\n\n            if (dpiY < 0) { dpiY = 600; }\n\n            AssertPositive(dpiX, \"PageResolution.ResolutionX\");\n            AssertPositive(dpiY, \"PageResolution.ResolutionY\");\n\n            int sizeX = (int) (_jobTicket.PageMediaSize.MediaSizeX * dpiX);\n            int sizeY = (int) (_jobTicket.PageMediaSize.MediaSizeY * dpiY);\n\n            AssertPositive(sizeX, \"PageCanvasSizeCap.CanvasSizeX\");\n            AssertPositive(sizeY, \"PageCanvasSizeCap.CanvasSizeY\");\n","sourceCodeStart":488,"sourceCodeEnd":524,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/printcontext.cs#L488-L524","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the Visual reference is non-null before calling Print/Render (check Loaded event completed)","Resolve the element by name after InitializeComponent and null-check the result","If printing a dynamically created visual, construct it (e.g. new FixedPage()) rather than passing an uninitialized variable","Wrap in a guard that throws a clearer app-level message when the visual cannot be resolved"],"exampleFix":"// before\nprintContext.Print(null);\n// after\nvar visual = this.FindName(\"ContentToPrint\") as Visual;\nif (visual == null) throw new InvalidOperationException(\"ContentToPrint was not found\");\nprintContext.Print(visual);","handlingStrategy":"type-guard","validationCode":"if (visual == null) throw new InvalidOperationException(\"Visual to print was not resolved\");","typeGuard":"bool IsPrintable(Visual v) => v != null && v.IsAncestorOf(v) || v != null;","tryCatchPattern":"try { printContext.Print(visual); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"visual\") { log.Error(\"Visual was null at print time\", ex); }","preventionTips":["Resolve named elements after InitializeComponent and null-check","Print only after the visual tree is Loaded","Never pass results of failed lookups directly to Print"],"tags":["wpf","printing","null-argument","visual"],"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"}