{"record":{"id":"7d99c53b4084e801","repo":"dotnet/wpf","slug":"throw-new-argumentexception-sr-getresourcestring-reason-null","errorCode":null,"errorMessage":"throw new ArgumentException(SR.GetResourceString(reason, null));","messagePattern":"throw new ArgumentException\\(SR\\.GetResourceString\\(reason, null\\)\\);","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationProvider/System/Windows/Automation/Provider/AutomationInteropProvider.cs","lineNumber":190,"sourceCode":"        //\n        //  Private Methods\n        //\n        //------------------------------------------------------\n\n        #region Private Methods\n\n        // Throw an argument Exception with a generic error\n        private static void ThrowInvalidArgument(string argName)\n        {\n            throw new ArgumentException(SR.Format(SR.GenericInvalidArgument, argName));\n        }\n\n        // Check that specified condition is true; if not, throw exception\n        private static void ValidateArgument(bool cond, string reason)\n        {\n            if (!cond)\n            {\n                throw new ArgumentException(SR.GetResourceString(reason, null));\n            }\n        }\n\n        #endregion Private Methods\n    }\n}\n","sourceCodeStart":172,"sourceCodeEnd":197,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationProvider/System/Windows/Automation/Provider/AutomationInteropProvider.cs#L172-L197","documentation":"AutomationInteropProvider.ValidateArgument throws ArgumentException with the resource string identified by 'reason' when a boolean validation condition fails. It guards the public entry points HostProviderFromHandle and ReturnRawElementProvider.","triggerScenarios":"Calling AutomationInteropProvider.HostProviderFromHandle or ReturnRawElementProvider with an invalid argument — e.g. a null or IntPtr.Zero handle, or a null client/provider object — failing the internal ValidateArgument condition.","commonSituations":"Hosting a custom UIA provider with an uncreated window handle; returning a raw element provider with a null element or null hwnd during window initialization.","solutions":["Read the exception's resource reason to see which argument failed","Ensure the hwnd passed to HostProviderFromHandle is non-zero and the window exists","Pass non-null provider/element/hwnd values to ReturnRawElementProvider","Add explicit null/IntPtr.Zero checks before calling these provider APIs"],"exampleFix":"// before\nreturn AutomationInteropProvider.HostProviderFromHandle(hwnd, provider);\n// after\nif (hwnd == IntPtr.Zero) throw new ArgumentNullException(nameof(hwnd));\nif (provider == null) throw new ArgumentNullException(nameof(provider));\nreturn AutomationInteropProvider.HostProviderFromHandle(hwnd, provider);","handlingStrategy":"validation","validationCode":"if (hwnd == IntPtr.Zero) throw new ArgumentNullException(nameof(hwnd));\nif (provider == null) throw new ArgumentNullException(nameof(provider));","typeGuard":"bool IsValidHandle(IntPtr h) => h != IntPtr.Zero;","tryCatchPattern":"try { var prov = AutomationInteropProvider.HostProviderFromHandle(hwnd, provider); }\ncatch (ArgumentException ex) { log.Error($\"Invalid provider argument: {ex.Message}\"); }","preventionTips":["Verify window handles are created before hosting providers","Pass non-null provider/element instances to ReturnRawElementProvider","Validate arguments at your own API boundary before delegating to AutomationInteropProvider"],"tags":["uiautomation","provider","argument","null"],"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"}