cefsharp/CefSharp · error · Exception

IBrowser instance is null. Browser has likely not finished i

Error message

IBrowser instance is null. Browser has likely not finished initializing or is in the process of disposing.

What it means

ThrowExceptionIfBrowserNull guards extension methods that operate on an IBrowser instance (such as the IBrowser overload of EvaluateScriptAsync). BrowserNullExceptionString fires when the IBrowser reference itself is null, indicating the underlying browser object is not available - usually because the browser hasn't finished initializing or is disposing.

Source

Thrown at CefSharp/WebBrowserExtensions.cs:1987

        /// <param name="frame">The <seealso cref="IFrame"/> instance this method extends.</param>
        public static void ThrowExceptionIfFrameNull(IFrame frame)
        {
            if (frame == null)
            {
                throw new Exception(FrameNullExceptionString);
            }
        }

        /// <summary>
        /// An IBrowser extension method that throw exception if browser null.
        /// </summary>
        /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
        /// <param name="browser">The ChromiumWebBrowser instance this method extends.</param>
        public static void ThrowExceptionIfBrowserNull(IBrowser browser)
        {
            if (browser == null)
            {
                throw new Exception(BrowserNullExceptionString);
            }
        }

        /// <summary>
        /// Throw exception if browser host null.
        /// </summary>
        /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
        /// <param name="browserHost">The browser host.</param>
        public static void ThrowExceptionIfBrowserHostNull(IBrowserHost browserHost)
        {
            if (browserHost == null)
            {
                throw new Exception(BrowserHostNullExceptionString);
            }
        }

        /// <summary>
        /// Throw exception if can execute javascript in main frame false.

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Resolve the IBrowser fresh at call time via browser.GetBrowser() and null-check it before use.
  2. Only call IBrowser methods after IsBrowserInitialized is true.
  3. Stop holding long-lived IBrowser references; obtain them on demand.
  4. Guard calls: if (ib != null) { ib.EvaluateScriptAsync(...); }.

Example fix

// before
var ib = browser.GetBrowser();
ib.EvaluateScriptAsync(script); // ib null -> throws

// after
var ib = browser.GetBrowser();
if (ib != null)
{
    ib.EvaluateScriptAsync(script);
}
Defensive patterns

Strategy: validation

Validate before calling

var ib = browser.GetBrowser();
if (ib != null)
{
    ib.EvaluateScriptAsync(script);
}

Type guard

static bool HasBrowser(IWebBrowser browser) => browser?.GetBrowser() != null;

Prevention

When it happens

Trigger: Calling IBrowser-keyed extension methods with a null IBrowser, e.g. passing browser.GetBrowser() when GetBrowser() returns null (host/browser not yet created), or chaining off a disposed browser whose IBrowser was cleared.

Common situations: Using GetBrowser() too early in the lifecycle. Keeping a stale IBrowser reference captured before initialization completed. Calling during shutdown when the browser host is being released.

Related errors


AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13). Data as JSON: /api/errors/3c77751563a91774. Report an issue: GitHub.