{"record":{"id":"f43fbfa6943b0b34","repo":"dotnet/maui","slug":"selector-cannot-be-null-or-whitespace","errorCode":null,"errorMessage":"'selector' cannot be null or whitespace.","messagePattern":"'selector' cannot be null or whitespace\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/BlazorWebView/src/WindowsForms/RootComponent.cs","lineNumber":26,"sourceCode":"\nnamespace Microsoft.AspNetCore.Components.WebView.WindowsForms\n{\n\t/// <summary>\n\t/// Describes a root component that can be added to a <see cref=\"BlazorWebView\"/>.\n\t/// </summary>\n\tpublic class RootComponent\n\t{\n\t\t/// <summary>\n\t\t/// Constructs an instance of <see cref=\"RootComponent\"/>.\n\t\t/// </summary>\n\t\t/// <param name=\"selector\">The CSS selector string that specifies where in the document the component should be placed. This must be unique among the root components within the <see cref=\"BlazorWebView\"/>.</param>\n\t\t/// <param name=\"componentType\">The type of the root component. This type must implement <see cref=\"IComponent\"/>.</param>\n\t\t/// <param name=\"parameters\">An optional dictionary of parameters to pass to the root component.</param>\n\t\tpublic RootComponent(string selector, Type componentType, IDictionary<string, object?>? parameters)\n\t\t{\n\t\t\tif (string.IsNullOrWhiteSpace(selector))\n\t\t\t{\n\t\t\t\tthrow new ArgumentException($\"'{nameof(selector)}' cannot be null or whitespace.\", nameof(selector));\n\t\t\t}\n\n\t\t\tSelector = selector;\n\t\t\tComponentType = componentType ?? throw new ArgumentNullException(nameof(componentType));\n\t\t\tParameters = parameters;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Gets the CSS selector string that specifies where in the document the component should be placed.\n\t\t/// This must be unique among the root components within the <see cref=\"BlazorWebView\"/>.\n\t\t/// </summary>\n\t\tpublic string Selector { get; }\n\n\t\t/// <summary>\n\t\t/// Gets the type of the root component. This type must implement <see cref=\"IComponent\"/>.\n\t\t/// </summary>\n\t\tpublic Type ComponentType { get; }\n","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/BlazorWebView/src/WindowsForms/RootComponent.cs#L8-L44","documentation":"The WinForms RootComponent constructor validates the selector parameter immediately (unlike the XAML-based MAUI/WPF variants which validate at add-time). Because WinForms constructs RootComponent in code with explicit constructor arguments, the validation can happen at construction. A null or whitespace selector means there is no CSS target in the host page for the component.","triggerScenarios":"Constructing 'new RootComponent(null, typeof(MyComponent), null)' or 'new RootComponent(\"   \", typeof(MyComponent), null)'. The ArgumentException fires before the object is fully constructed, so the RootComponent is never added to the collection.","commonSituations":"Passing a variable that resolved to null or empty string as the selector; copy-paste from example code where selector was a placeholder; refactoring that introduced a null selector from a configuration value; unit test creating RootComponent with test data that included an empty selector.","solutions":["Pass a valid non-empty CSS selector string as the first constructor argument.","If selector comes from configuration, validate it before constructing: if (string.IsNullOrWhiteSpace(selector)) throw new InvalidOperationException(\"Selector must be configured.\");","Use a constant or well-known selector like \"#app\" or \"#blazor-root\".","Audit all RootComponent constructor calls for null or empty selectors."],"exampleFix":"// before\nvar rc = new RootComponent(\"\", typeof(MyComponent), null);\n// after\nvar rc = new RootComponent(\"#app\", typeof(MyComponent), null);","handlingStrategy":"validation","validationCode":"// Validate selector before constructing RootComponent\nif (string.IsNullOrWhiteSpace(selector))\n{\n    throw new ArgumentException(\"Selector must be a non-empty CSS selector string.\", nameof(selector));\n}\nvar rc = new RootComponent(selector, componentType, parameters);","typeGuard":"static bool IsValidSelector(string selector)\n{\n    return !string.IsNullOrWhiteSpace(selector);\n}","tryCatchPattern":null,"preventionTips":["Use a constant or well-known selector (e.g. \"#app\") in all RootComponent constructor calls.","If selector comes from configuration, validate it at startup before constructing RootComponent.","Add a unit test that constructs RootComponent with typical selector values.","Audit constructor calls during code review for missing or empty selectors."],"tags":["blazor","blazorwebview","winforms","rootcomponent","validation","constructor"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}