{"record":{"id":"3082356abb8b4732","repo":"dotnet/wpf","slug":"argumentnullexception-nameof-text","errorCode":null,"errorMessage":"ArgumentNullException(nameof(text))","messagePattern":"ArgumentNullException\\(nameof\\(text\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs","lineNumber":170,"sourceCode":"        public void NavigateToStream(Stream stream)\n        {\n            ArgumentNullException.ThrowIfNull(stream);\n\n            DocumentStream = stream;            \n            // We navigate to \"about:blank\" when Source is set to null. \n            // When we get NavigateComplete event, we load the stream via the IPersistStreamInit interface.\n            Source = null;\n        }\n\n        /// <summary>\n        /// Navigates to the text of a html page.\n        /// </summary>\n        /// <param name=\"text\">The string that contains the content of a html document</param>\n        public void NavigateToString(String text)\n        {\n            if (string.IsNullOrEmpty(text))\n            {\n                throw new ArgumentNullException(nameof(text));\n            }\n\n            MemoryStream ms = new MemoryStream(text.Length);\n            StreamWriter sw = new StreamWriter(ms);\n            sw.Write(text);\n            sw.Flush();\n            ms.Position = 0;\n\n            NavigateToStream(ms);\n        }\n\n        /// <summary>\n        /// Navigates the WebBrowser control to the previous page if available.\n        /// </summary>\n        public void GoBack()\n        {\n            VerifyAccess();\n","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs#L152-L188","documentation":"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.","triggerScenarios":"Calling webBrowser.NavigateToString(null) or NavigateToString(\"\") — any null/empty/whitespace-free string variable (e.g. a failed load from a file or HTTP response).","commonSituations":"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.","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."],"exampleFix":"// before\nwebBrowser.NavigateToString(html);\n// after\nif (!string.IsNullOrEmpty(html))\n    webBrowser.NavigateToString(html);\nelse\n    webBrowser.NavigateToString(\"<html><body><p>No content</p></body></html>\");","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(text)) { /* skip or use fallback */ }","typeGuard":"bool CanNavigate(string s) => !string.IsNullOrEmpty(s);","tryCatchPattern":"try { webBrowser.NavigateToString(text); } catch (ArgumentNullException) { webBrowser.NavigateToString(\"<html><body></body></html>\"); }","preventionTips":["Validate HTML strings at their source","Use a non-empty fallback document"],"tags":["wpf","webbrowser","argumentnull","html"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}