Darkmatter5
Darkmatter5

Reputation: 1249

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.

@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.

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

Thank you for whatever help you can give me.

Upvotes: 1

Views: 9912

Answers (1)

j.loucao.silva
j.loucao.silva

Reputation: 390

I'd say Pieces is null

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

Should help, or go overkill and check the PieceService for null as well

Upvotes: 3

Related Questions