Blazor WASM 应用程序中的对象引用未设置错误

Object reference not set error in Blazor WASM app

我很难找到以下错误代码的原因。

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
  Unhandled exception rendering component: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
  at MusicManager.Client.Pages.Pieces.BuildRenderTree(RenderTreeBuilder __builder) in D:\MusicManager\MusicManager\Client\Pages\Pieces.razor:line 20
  at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)
  at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)

这是引用的 Pieces.razor 文件中的代码。

@page "/pieces"
@inject IPieceService PieceService
@inject NavigationManager NavigationManager
@implements IDisposable 

<PageTitle>Pieces</PageTitle>
<h3>Pieces</h3>
<table class="table table-hover">
    <thead>
        <tr>
            <th scope="col" style="width: 100px">#</th>
            <th scope="col">Title</th>
            <th scope="col">Number of Copies</th>
            <th scope="col">Score?</th>
            <th scope="col">Last Performed</th>
            <th scope="col"></th>
        </tr>
    </thead>
    <tbody>
        @if (PieceService.Pieces.Count == 0)
        {
            <tr><td colspan="6"><span>Loading Pieces...</span></td></tr>
        }
        else
        {
            @foreach (var piece in PieceService.Pieces)
            {
                <tr>
                    <td>@piece.Id</td>
                    <td>@piece.Title</td>
                    <td>@piece.NumOfCopies</td>
                    <td>@piece.ScoreYesNo</td>
                    <td>@piece.LastPerformed</td>
                    <td align="right">
                        <button class="btn btn-primary" @onclick="(() =>     Show(piece.Id))">Show</button>
                        <button class="btn btn-primary" @onclick="(() => Edit(piece.Id))">Edit</button>
                        <button class="btn btn-primary" @onclick="(() => Delete(piece.Id))">Delete</button>
                    </td>
                </tr>
            }
        }
    </tbody>
</table>

<FormPiece piece="pieceToEdit"></FormPiece>

@code {
    Piece pieceToEdit = new Piece();

    protected override async Task OnInitializedAsync()
    {
        await PieceService.GetAll();
        PieceService.OnChange += StateHasChanged;
    }

    void Show(int id)
    {
        NavigationManager.NavigateTo($"pieces/{id}");
    }

    void Edit(int id)
    {
        pieceToEdit = PieceService.Pieces.Find(x => x.Id == id);
    }

    void Delete(int id)
    {
        PieceService.Delete(id);
    }

    public void Dispose()
    {
        PieceService.OnChange -= StateHasChanged;
    }
}

如果需要,我会包含更多代码,但我认为错误出在这段代码中。该页面呈现正常,但该错误除外。表单甚至连同删除、显示和编辑按钮一起正确提交。

错误中引用的第 20 行指向这一行。

@if (PieceService.Pieces.Count == 0)

谢谢你能给我的任何帮助。

我会说 Pieces 为空

@if (PieceService.Pieces?.Count == 0)

应该有帮助,或者矫枉过正并检查 PieceService 是否为 null