{"record":{"id":"e2aec0ddfea6ba36","repo":"cefsharp/CefSharp","slug":"ibrowser-instance-is-null-browser-has-likely-not","errorCode":null,"errorMessage":"IBrowser instance is null. Browser has likely not finished initializing or is in the process of disposing.","messagePattern":"IBrowser 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.WinForms/Host/ChromiumHostControl.cs","lineNumber":270,"sourceCode":"\n        /// <inheritdoc/>\n        public Task<WaitForNavigationAsyncResponse> WaitForNavigationAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)\n        {\n            //WaitForNavigationAsync is actually a static method so that CefSharp.Wpf.HwndHost can reuse the code\n            return CefSharp.WebBrowserExtensions.WaitForNavigationAsync(this, timeout, cancellationToken);\n        }\n\n        /// <summary>\n        /// Returns the main (top-level) frame for the browser window.\n        /// </summary>\n        /// <returns> the main frame</returns>\n        public IFrame GetMainFrame()\n        {\n            var browser = BrowserCore;\n\n            if(browser == null)\n            {\n                throw new Exception(CefSharp.WebBrowserExtensions.BrowserNullExceptionString);\n            }\n\n            return browser.MainFrame;\n        }\n\n        /// <inheritdoc/>\n        protected override void Dispose(bool disposing)\n        {\n            if (disposing)\n            {\n                var browserCore = BrowserCore;\n\n                AddressChanged = null;\n                ConsoleMessage = null;\n                FrameLoadEnd = null;\n                FrameLoadStart = null;\n                LoadError = null;\n                LoadingStateChanged = null;","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.WinForms/Host/ChromiumHostControl.cs#L252-L288","documentation":"Thrown by ChromiumHostControl.GetMainFrame() when BrowserCore is null, using the shared BrowserNullExceptionString constant. BrowserCore is null before the browser has finished initializing and is set to null during Dispose. The method has no valid MainFrame to return in either state, so it fails fast rather than returning a null dereference downstream.","triggerScenarios":"Calling GetMainFrame() before IsBrowserInitialized is true; calling it after Dispose has nulled BrowserCore; calling from a teardown or closing path.","commonSituations":"Reaching for the main frame to execute script or get the URL during early startup; background tasks invoking GetMainFrame after the user closed the control; chaining GetMainFrame().ExecuteScriptAsync without a null guard.","solutions":["Guard with `if (hostControl.BrowserCore != null)` before calling GetMainFrame().","Wait for IsBrowserInitialized (subscribe or await) before touching the main frame.","Cancel background work on dispose so GetMainFrame is never called after teardown.","Use the async extension methods (e.g. GetMainFrameAsync / WaitForNavigationAsync) which handle the null core case."],"exampleFix":"// before\nvar frame = hostControl.GetMainFrame(); // throws if BrowserCore null\nvar src = frame.Url;\n\n// after\nif (hostControl.BrowserCore != null)\n{\n    var frame = hostControl.GetMainFrame();\n    var src = frame.Url;\n}","handlingStrategy":"validation","validationCode":"if (hostControl.BrowserCore != null)\n{\n    var frame = hostControl.GetMainFrame();\n}","typeGuard":"static bool IsCoreAlive(ChromiumHostControl c) => c?.BrowserCore != null;","tryCatchPattern":null,"preventionTips":["Wait for IsBrowserInitialized before touching the main frame.","Cancel background work on dispose so GetMainFrame is not called after teardown.","Prefer async extensions (GetMainFrameAsync) that handle the null core case."],"tags":["cefwsharp","winforms","lifecycle","mainframe"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}