{"record":{"id":"539a02b21e85647e","repo":"cefsharp/CefSharp","slug":"ibrowserhost-instance-is-null-browser-has-likely","errorCode":null,"errorMessage":"IBrowserHost instance is null. Browser has likely not finished initializing or is in the process of disposing.","messagePattern":"IBrowserHost instance is null\\. Browser has likely not finished initializing or is in the process of disposing\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp/WebBrowserExtensions.cs","lineNumber":2000,"sourceCode":"        /// <param name=\"browser\">The ChromiumWebBrowser instance this method extends.</param>\n        public static void ThrowExceptionIfBrowserNull(IBrowser browser)\n        {\n            if (browser == null)\n            {\n                throw new Exception(BrowserNullExceptionString);\n            }\n        }\n\n        /// <summary>\n        /// Throw exception if browser host null.\n        /// </summary>\n        /// <exception cref=\"Exception\">Thrown when an exception error condition occurs.</exception>\n        /// <param name=\"browserHost\">The browser host.</param>\n        public static void ThrowExceptionIfBrowserHostNull(IBrowserHost browserHost)\n        {\n            if (browserHost == null)\n            {\n                throw new Exception(BrowserHostNullExceptionString);\n            }\n        }\n\n        /// <summary>\n        /// Throw exception if can execute javascript in main frame false.\n        /// </summary>\n        /// <exception cref=\"Exception\">Thrown when an exception error condition occurs.</exception>\n        public static void ThrowExceptionIfCanExecuteJavascriptInMainFrameFalse()\n        {\n            throw new Exception(\"Unable to execute javascript at this time, scripts can only be executed within a V8Context. \" +\n                                    \"Use the IWebBrowser.CanExecuteJavascriptInMainFrame property to guard against this exception. \" +\n                                    \"See https://github.com/cefsharp/CefSharp/wiki/General-Usage#when-can-i-start-executing-javascript \" +\n                                    \"for more details on when you can execute javascript. For frames that do not contain Javascript then no \" +\n                                    \"V8Context will be created. Executing a script once the frame has loaded it's possible to create a V8Context. \" +\n                                    \"You can use browser.GetMainFrame().ExecuteJavaScriptAsync(script) or browser.GetMainFrame().EvaluateScriptAsync \" +\n                                    \"to bypass these checks (advanced users only).\");\n        }\n    }","sourceCodeStart":1982,"sourceCodeEnd":2018,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp/WebBrowserExtensions.cs#L1982-L2018","documentation":"ThrowExceptionIfBrowserHostNull fires when an extension method needs the IBrowserHost (e.g. GetCookieManager, show dev tools, print) but browser.GetBrowserHost() returns null. The host is the live CEF browser handle; a null host means the underlying browser process hasn't created the host yet or has already torn it down during disposal.","triggerScenarios":"Calling host-dependent extension methods when GetBrowserHost() returns null - before IsBrowserInitialized or during/after disposal. Common in OffScreen/WinForms hosts accessed synchronously right after construction.","commonSituations":"Calling GetCookieManager, ShowDevTools, PrintToPdfAsync, or similar immediately after new ChromiumWebBrowser() before the browser host exists. Accessing the host from a Form.Closing or disposed event handler.","solutions":["Wait until IsBrowserInitialized is true before invoking any host-dependent method.","Null-check the host: var host = browser.GetBrowserHost(); if (host != null) { ... }.","Avoid calling host methods from disposing handlers.","For OffScreen, use the WaitForInitialization() pattern before touching the host."],"exampleFix":"// before\nvar host = browser.GetBrowserHost();\nhost.ShowDevTools(); // host null -> throws\n\n// after\nif (browser.IsBrowserInitialized)\n{\n    var host = browser.GetBrowserHost();\n    if (host != null)\n    {\n        host.ShowDevTools();\n    }\n}","handlingStrategy":"validation","validationCode":"var host = browser.GetBrowserHost();\nif (host != null)\n{\n    host.ShowDevTools();\n}","typeGuard":"static bool HasBrowserHost(IWebBrowser browser) => browser?.GetBrowserHost() != null;","tryCatchPattern":null,"preventionTips":["Gate host-dependent calls on IsBrowserInitialized.","Null-check the host at every call site in lifecycle-sensitive code.","For OffScreen hosts, await initialization before accessing the host."],"tags":["cefsharp","browser-host","lifecycle","initialization"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}