cefsharp/CefSharp · error · ArgumentNullException

browser

Error message

browser

What it means

The message 'browser' is the parameter name from throw new ArgumentNullException(nameof(browser)) inside ThrowExceptionIfChromiumWebBrowserDisposed. It fires when the IWebBrowser passed to an extension method is a null reference - the method cannot check IsDisposed on null, so it rejects the null argument before any disposal logic. (Note: a disposed browser instead throws ObjectDisposedException with the type name, a different message.)

Source

Thrown at CefSharp/WebBrowserExtensions.cs:1952

                    }

                    stringBuilder.Append(", ");
                }

                //Remove the trailing comma
                stringBuilder.Remove(stringBuilder.Length - 2, 2);
            }

            stringBuilder.Append(");");

            return stringBuilder.ToString();
        }

        public static void ThrowExceptionIfChromiumWebBrowserDisposed(IChromiumWebBrowserBase browser)
        {
            if (browser == null)
            {
                throw new ArgumentNullException(nameof(browser));
            }

            if (browser.IsDisposed)
            {
                // Provide a more meaningful message for WinForms ChromiumHostControl
                // should be ChromiumWebBrowser/ChromiumHostControl
                var type = browser.GetType();

                throw new ObjectDisposedException(type.Name);
            }
        }

        /// <summary>
        /// Throw exception if frame null.
        /// </summary>
        /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
        /// <param name="frame">The <seealso cref="IFrame"/> instance this method extends.</param>
        public static void ThrowExceptionIfFrameNull(IFrame frame)

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Ensure the ChromiumWebBrowser instance is created before invoking any extension method on it.
  2. Null-check the browser reference before calling: if (browser != null) browser.Load(url);
  3. Fix the object lifecycle: assign the field in InitializeComponent/onInitialized rather than leaving it null.
  4. If using DI, verify the IWebBrowser registration resolves a live instance at the call site.

Example fix

// before
browser.Load(url); // browser is null -> ArgumentNullException 'browser'

// after
if (browser != null)
{
    browser.Load(url);
}
Defensive patterns

Strategy: validation

Validate before calling

if (browser != null && !browser.IsDisposed)
{
    browser.Load(url);
}

Type guard

static bool IsUsable(IWebBrowser browser) => browser != null && !browser.IsDisposed;

Prevention

When it happens

Trigger: Invoking any CefSharp extension method guarded by ThrowExceptionIfChromiumWebBrowserDisposed (e.g. Load, RegisterResourceHandler, UnRegisterResourceHandler, GetCookieManager) with a null IWebBrowser instance - typically a not-yet-assigned field or a disposed-and-nulled reference.

Common situations: Calling extension methods on a browser field before it is assigned (e.g. in a constructor or Form.Load that runs before the ChromiumWebBrowser is created). Using a nullable reference that was set to null after disposal. DI/scoped lifetimes where the browser instance was never resolved.

Related errors


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