简体   繁体   English

Blazor WASM 应用程序中的 Object 参考未设置错误

[英]Object reference not set error in Blazor WASM app

I'm having a hard time tracking down the cause of the following error code.我很难追踪以下错误代码的原因。

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)

Here is the code from the Pieces.razor file referenced.以下是引用的 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;
    }
}

I will include more code if requested, but I think the error resides within this code.如果需要,我将包含更多代码,但我认为错误存在于此代码中。 The page renders fine, with the exception of that error.该页面呈现正常,但该错误除外。 The form even submits properly along with the delete, show and edit buttons.表单甚至与删除、显示和编辑按钮一起正确提交。

Line 20, referenced in the error is pointing to this line.错误中引用的第 20 行指向此行。

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

Thank you for whatever help you can give me.谢谢你能给我的任何帮助。

I'd say Pieces is null我会说 Pieces 是 null

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

Should help, or go overkill and check the PieceService for null as well应该有帮助,或者 go 矫枉过正,并检查 null 的 PieceService

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM