dotnet/wpf · error · ArgumentNullException
ArgumentNullException(nameof(text))
Error message
ArgumentNullException(nameof(text))
What it means
WebBrowser.NavigateToString throws ArgumentNullException (despite checking IsNullOrEmpty) when the text argument is null or empty. The method renders an HTML string in the WebBrowser control, and empty content cannot be navigated to.
Solutions
- Check string.IsNullOrEmpty(text) before calling and skip or substitute placeholder HTML.
- Provide fallback content such as "<html><body></body></html>" when source is empty.
- Fix the upstream producer so the HTML string is populated before navigation.
- Wrap in try-catch (ArgumentNullException) only when the emptiness is expected and ignorable.
Example fix
// before
webBrowser.NavigateToString(html);
// after
if (!string.IsNullOrEmpty(html))
webBrowser.NavigateToString(html);
else
webBrowser.NavigateToString("<html><body><p>No content</p></body></html>"); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(text)) { /* skip or use fallback */ } Type guard
bool CanNavigate(string s) => !string.IsNullOrEmpty(s);
Try / catch
try { webBrowser.NavigateToString(text); } catch (ArgumentNullException) { webBrowser.NavigateToString("<html><body></body></html>"); } Prevention
- Validate HTML strings at their source
- Use a non-empty fallback document
When it happens
Trigger: Calling webBrowser.NavigateToString(null) or NavigateToString("") — any null/empty/whitespace-free string variable (e.g. a failed load from a file or HTTP response).
Common situations: Passing result of File.ReadAllText on an empty file; passing an empty HTTP response body; data-bound property that is still null at navigation time.
Related errors
- ArgumentNullException(nameof(scriptName))
- ArgumentNullException: child
- ArgumentNullException: hitTestParameters
- ArgumentNullException(paramName)
- block
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/3082356abb8b4732.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs:170
public void NavigateToStream(Stream stream)
{
ArgumentNullException.ThrowIfNull(stream);
DocumentStream = stream;
// We navigate to "about:blank" when Source is set to null.
// When we get NavigateComplete event, we load the stream via the IPersistStreamInit interface.
Source = null;
}
/// <summary>
/// Navigates to the text of a html page.
/// </summary>
/// <param name="text">The string that contains the content of a html document</param>
public void NavigateToString(String text)
{
if (string.IsNullOrEmpty(text))
{
throw new ArgumentNullException(nameof(text));
}
MemoryStream ms = new MemoryStream(text.Length);
StreamWriter sw = new StreamWriter(ms);
sw.Write(text);
sw.Flush();
ms.Position = 0;
NavigateToStream(ms);
}
/// <summary>
/// Navigates the WebBrowser control to the previous page if available.
/// </summary>
public void GoBack()
{
VerifyAccess();
View on GitHub (pinned to 81131a70a4)