dotnet / dotnet/aspnetcore

Blazor InputSelect with multiple enabled throws NullRefException if nothing is selected

Open
#54,431 5 comments 0 reactions 0 assignees View on GitHub
area-blazor bug help wanted
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

If there is an InputSelect in an EditForm with multiple values (via array) and nothing is selected (or everything is deselected) the following exception is thrown:
```plain
System.NullReferenceException: Object reference not set to an instance of an object.

at Microsoft.AspNetCore.Components.BindConverter.FormatterDelegateCache.<>c__DisplayClass3_0`1.g__FormatArrayValue|0(T[] value, CultureInfo culture)

at Microsoft.AspNetCore.Components.Forms.InputSelect`1.BuildRenderTree(RenderTreeBuilder builder)

at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
```

Assuming the following model
```csharp
{
public List<(bool, string)> CurrentRoles { get; set; } = new List<(bool, string)>();
public string[] SelectedRoles { get; set; } = [];
}
```
And the following markup:
```csharp


Roles

@if(Model.CurrentRoles is not null)
{
@foreach (var (selected, role) in Model.CurrentRoles)
{
@role
}
}


```
Repo see below, markup is here: https://github.com/Tornhoof/InputMultiSelectRepro/blob/main/Components/Pages/Home.razor
### Expected Behavior

No such exception

Workaround is to set the array to [] in ``OnInitializedAsync``

### Steps To Reproduce

1. checkout https://github.com/Tornhoof/InputMultiSelectRepro
2. make sure line 76 (Model.SelectedRoles ??= []) in Home.razor is commented
3. Start project
4. Deselect FirstRole in the Input Select Field

### Exceptions (if any)
```plain
System.NullReferenceException: Object reference not set to an instance of an object.

at Microsoft.AspNetCore.Components.BindConverter.FormatterDelegateCache.<>c__DisplayClass3_0`1.g__FormatArrayValue|0(T[] value, CultureInfo culture)

at Microsoft.AspNetCore.Components.Forms.InputSelect`1.BuildRenderTree(RenderTreeBuilder builder)

at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)

--- End of stack trace from previous location ---

at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()

--- End of stack trace from previous location ---

at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()

at Microsoft.AspNetCore.Components.RenderTree.Renderer.AddToRenderQueue(Int32 componentId, RenderFragment renderFragment)

at Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged()

at Microsoft.AspNetCore.Components.ComponentBase.CallOnParametersSetAsync()

at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

at Microsoft.AspNetCore.Components.Rendering.ComponentState.SetDirectParameters(ParameterView parameters)

at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderRootComponentAsync(Int32 componentId, ParameterView initialParameters)

at Microsoft.AspNetCore.Components.HtmlRendering.Infrastructure.StaticHtmlRenderer.BeginRenderingComponent(IComponent component, ParameterView initialParameters)

at Microsoft.AspNetCore.Components.Endpoints.EndpointHtmlRenderer.RenderEndpointComponent(HttpContext httpContext, Type rootComponentType, ParameterView parameters, Boolean waitForQuiescence)

at Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContext context)

at Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContext context)

at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<b__10_0>d.MoveNext()

--- End of stack trace from previous location ---

at Microsoft.AspNetCore.Antiforgery.Internal.AntiforgeryMiddleware.InvokeAwaited(HttpContext context)

at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)

at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

```
### .NET Version

8.0.200

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.