{"record":{"id":"09a277b54cf34906","repo":"dotnet/wpf","slug":"argumentnullexception-hwnd","errorCode":null,"errorMessage":"ArgumentNullException (hwnd)","messagePattern":"ArgumentNullException \\(hwnd\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs","lineNumber":43,"sourceCode":"        IRawElementProviderFragment,\n        IWindowProvider,\n        ITransformProvider\n    {\n        //------------------------------------------------------\n        //\n        //  Constructors\n        //\n        //------------------------------------------------------\n\n        #region Constructors\n\n        internal HwndProxyElementProvider( NativeMethods.HWND hwnd )\n        {\n            Debug.Assert( hwnd != NativeMethods.HWND.NULL );\n\n            if( hwnd == NativeMethods.HWND.NULL )\n            {\n                throw new ArgumentNullException( nameof(hwnd));\n            }\n\n            _hwnd = hwnd;\n        }\n\n        #endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  Interface IRawElementProviderSimple/Fragment/Root\n        //\n        //------------------------------------------------------\n\n        #region IRawElementProviderSimple\n\n        ProviderOptions IRawElementProviderSimple.ProviderOptions\n        {\n            get","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs#L25-L61","documentation":"The UIAutomation HwndProxyElementProvider constructor asserts and then throws ArgumentNullException when the hwnd argument is HWND.NULL. A proxy element provider must wrap a real window handle; a null/NULL handle cannot identify any automation element.","triggerScenarios":"Constructing HwndProxyElementProvider (directly or via provider factory code) with NativeMethods.HWND.NULL, e.g. when a preceding window lookup (WindowFromPoint, RealGetWindow, etc.) returned NULL and the result was passed through unchecked.","commonSituations":"UIA proxying for a window that has already been destroyed; querying UIA at a screen point where no window exists; race with window teardown during automation tests.","solutions":["Check hwnd != HWND.NULL before constructing and skip/skip-proxy when null.","Verify the target window still exists (IsWindow) before obtaining a provider.","Re-query the element after the window is recreated or use UIA's element-from-point APIs that handle the no-window case."],"exampleFix":"// before\nvar provider = new HwndProxyElementProvider(hwnd); // hwnd may be NULL\n// after\nif (hwnd == NativeMethods.HWND.NULL) return null;\nvar provider = new HwndProxyElementProvider(hwnd);","handlingStrategy":"type-guard","validationCode":"if (hwnd == NativeMethods.HWND.NULL)\n    return null; // no window to proxy","typeGuard":"static bool IsValidHwnd(NativeMethods.HWND hwnd) => hwnd != NativeMethods.HWND.NULL && SafeNativeMethods.IsWindow(hwnd);","tryCatchPattern":"try { var p = new HwndProxyElementProvider(hwnd); }\ncatch (ArgumentNullException) { /* skip; window handle was null */ }","preventionTips":["Always check Win32 window-lookup results for NULL before wrapping them.","Re-validate with IsWindow after any async/remote step.","Handle window teardown races in automation loops."],"tags":["uiautomation","null-argument","interop"],"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"}