{"record":{"id":"205a1c3578f39179","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-scriptname","errorCode":null,"errorMessage":"ArgumentNullException(nameof(scriptName))","messagePattern":"ArgumentNullException\\(nameof\\(scriptName\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs","lineNumber":257,"sourceCode":"        /// <returns>The object returned by the Active Scripting call.</returns>\n        public object InvokeScript(string scriptName)\n        {\n            return InvokeScript(scriptName, null);\n        }\n\n        /// <summary>\n        /// Executes an Active Scripting function defined in the HTML document currently loaded in the WebBrowser control. \n        /// </summary>\n        /// <param name=\"scriptName\">The name of the script method to invoke.</param>\n        /// <param name=\"args\"></param>\n        /// <returns>The object returned by the Active Scripting call.</returns>\n        public object InvokeScript(string scriptName, params object[] args)\n        {\n            VerifyAccess();\n\n            if (string.IsNullOrEmpty(scriptName))\n            {\n                throw new ArgumentNullException(nameof(scriptName));\n            }\n\n            UnsafeNativeMethods.IDispatchEx scriptObjectEx = null;\n            UnsafeNativeMethods.IHTMLDocument2 htmlDocument = NativeHTMLDocument;\n            if (htmlDocument != null)\n            {\n                scriptObjectEx = htmlDocument.GetScript() as UnsafeNativeMethods.IDispatchEx;\n            }\n\n            // Protect against the cross domain scripting attacks. \n            // We rely on the site locking feature in gerneral. But in IE 6 server side redirect is not blocked.\n            // (In IE 7 it is blocked by turning on the DOCHOSTUIFLAG.ENABLE_REDIRECT_NOTIFICATION flag so that  \n            // the additional BeforeNavigate2 event is fired for server side redirect.)\n\n            object retVal = null;            \n            if (scriptObjectEx != null)\n            {\n                NativeMethods.DISPPARAMS dp = new NativeMethods.DISPPARAMS","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs#L239-L275","documentation":"WebBrowser.InvokeScript throws ArgumentNullException when scriptName is null or empty, after verifying thread access. A script function name is required to locate the dispatch target on the hosted document.","triggerScenarios":"Calling webBrowser.InvokeScript(null, args) or InvokeScript(\"\", args); passing a name from a config/binding that resolved to empty.","commonSituations":"Invoking script before page load where the name comes from parsed metadata; empty setting in app config; refactoring that lost the script name constant.","solutions":["Validate the script name: if (string.IsNullOrEmpty(name)) return/skip before calling.","Ensure the constant/binding supplying the name is non-empty.","Check the document finished loading (LoadCompleted) before invoking so the right name is used.","Wrap in try-catch (ArgumentNullException) for dynamic callers."],"exampleFix":"// before\nwebBrowser.InvokeScript(scriptName, args);\n// after\nif (!string.IsNullOrEmpty(scriptName))\n    webBrowser.InvokeScript(scriptName, args);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(scriptName)) return null;","typeGuard":"bool CanInvoke(string name) => !string.IsNullOrEmpty(name);","tryCatchPattern":"try { result = webBrowser.InvokeScript(name, args); } catch (ArgumentNullException) { result = null; }","preventionTips":["Keep script names as validated constants","Check config/bindings that supply names"],"tags":["wpf","webbrowser","argumentnull","javascript"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}