{"record":{"id":"0ef6143ab8dc5dc7","repo":"dotnet/wpf","slug":"dpiinfo-cannot-be-null","errorCode":null,"errorMessage":"dpiInfo cannot be null","messagePattern":"dpiInfo cannot be null","errorType":"exception","errorClass":"NullReferenceException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualTreeHelper.cs","lineNumber":106,"sourceCode":"        }\n\n        /// <summary>\n        /// Returns the DPI information at which this Visual is measured and rendered.\n        /// </summary>\n        public static DpiScale GetDpi(Visual visual)\n        {\n            return visual.GetDpi();\n        }\n\n\n        /// <summary>\n        /// This method updates the DPI information of a visual. It can only be called on a Visual with no parent.\n        /// </summary>\n        public static void SetRootDpi(Visual visual, DpiScale dpiInfo)\n        {\n            if ((object)dpiInfo == null)\n            {\n                throw new NullReferenceException(\"dpiInfo cannot be null\");\n            }\n            if (visual.InternalVisualParent != null)\n            {\n                throw new InvalidOperationException(\"UpdateDPI should only be called on the root of a Visual tree\");\n            }\n            DpiFlags dpiFlags = DpiUtil.UpdateDpiScalesAndGetIndex(dpiInfo.PixelsPerInchX, dpiInfo.PixelsPerInchY);\n            visual.RecursiveSetDpiScaleVisualFlags(new DpiRecursiveChangeArgs(dpiFlags, visual.GetDpi(), dpiInfo));\n        }\n\n        /// <summary>\n        /// Visual parent of this Visual.\n        /// </summary>\n        public static DependencyObject GetParent(DependencyObject reference)\n        {\n            Visual visual;\n            Visual3D visual3D;\n\n            VisualTreeUtils.AsNonNullVisual(reference, out visual, out visual3D);","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualTreeHelper.cs#L88-L124","documentation":"VisualTreeHelper.SetRootDpi throws NullReferenceException when the dpiInfo parameter is null. The method needs the DpiScale to propagate to the visual subtree and cannot proceed without it. (The null check pattern (object)dpiInfo == null throws NRE rather than ArgumentNullException, matching the original WPF source.)","triggerScenarios":"Calling VisualTreeHelper.SetRootDpi(visual, null) — e.g. when a DPI-changed handler passes a default(DpiScale) struct boxed as null, or a variable that was never assigned.","commonSituations":"Top-level HwndSource/window DPI-change plumbing where DpiScale came from a nullable wrapper that was null; automation/tests invoking SetRootDpi directly without computing a valid DpiScale.","solutions":["Ensure a valid DpiScale is computed (e.g. from VisualTreeHelper.GetDpi(visual) or the source's DpiChanged event args) before calling","Guard the call: if (dpiInfo == null) return or substitute a fallback DpiScale","Verify the calling code passes the NewDpi/OldDpi property from DpiChangedEventArgs, not a nullable that can be null"],"exampleFix":"// before\nvisualTreeHelper.SetRootDpi(rootVisual, maybeDpi); // maybeDpi is null -> throws\n// after\nif (maybeDpi != null) VisualTreeHelper.SetRootDpi(rootVisual, maybeDpi.Value);","handlingStrategy":"validation","validationCode":"if (dpiInfo == null)\n    throw new ArgumentNullException(nameof(dpiInfo)); // fail fast at your boundary","typeGuard":"bool IsValidDpi(DpiScale? dpi) => dpi.HasValue && dpi.Value.PixelsPerInchX > 0 && dpi.Value.PixelsPerInchY > 0;","tryCatchPattern":"try { VisualTreeHelper.SetRootDpi(root, dpi); }\ncatch (NullReferenceException) { /* substitute default DPI, e.g. VisualTreeHelper.GetDpi(root) */ }","preventionTips":["Always source DpiScale from DpiChangedEventArgs.NewDpi or GetDpi, never unassigned variables","Null-check DPI arguments at your API boundary before calling WPF internals","Treat NRE from WPF DPI APIs as a caller bug, not transient"],"tags":["wpf","dpi","null-reference","visualtree"],"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"}