IvanJosipovic / BlazorTable

Blazor Table Component with Sorting, Paging and Filtering
https://BlazorTable.netlify.app
MIT License
540 stars 109 forks source link

Updating loader causes a null reference exception [Bug] #282

Open KevinBurton opened 3 years ago

KevinBurton commented 3 years ago

Describe the bug I want to be able to page through a table. But the parameters to get the table are changing so the parameters to the constructor of the loader are 'stale'. I am using a state management library that re-renders the component when the parameters change. So I re-constructed the loader on the OnParametersSetAsync Blazor lifecycle hook. when I do that I get a null reference exception that seems to come from rendering the table.

To Reproduce Please create a standalone Page which reproduces the issue and paste the code here in a code block.

There are three standalone components involved. The main component looks like

@using BlazorTable

<h3>Breakpoint Grouping</h3>
<h4>Standards</h4>
@BreakpointStandardCmp
<h4>Groups</h4>
@BreakpointGroupPickerCmp

@if (string.IsNullOrWhiteSpace(Props.Standard.Bpstandard) ||
   string.IsNullOrWhiteSpace(Props.Group.BpgroupName))
{
    <h3> Standard or Group not selected</h3>
}
else
{
    <h3>Organisms NOT in this group @Props.Standard.BpstandardId, Props.Group.BpGroupId</h3>
    <Table TableItem="OrganismName" DataLoader="_groupingExcludedLoader" Items="_groupingExcludedData" PageSize="5" SelectionType="selectionType" SelectedItems="_selectedExcludedOrganisms" ShowSearchBar="false">
        <Column TableItem="OrganismName" Title="Organism Id" Field="@(x => x.OrganismId)" Sortable="true" Width="10%" DefaultSortColumn="true" />
        <Column TableItem="OrganismName" Title="Organism Name" Field="@(x => x.Name)" Sortable="true" Width="20%" />
        <Column TableItem="OrganismName" Title="Family Id" Field="@(x => x.OrganismFamilyId)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="SubFamily Id" Field="@(x => x.OrganismSubfamilyId)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Genus Id" Field="@(x => x.GenusId)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Print On Code List" Field="@(x => x.PrintOnCodeList)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Remap To" Field="@(x => x.RemapTo)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Aerobic" Field="@(x => x.Aerobic)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Anaerobic" Field="@(x => x.Anaerobic)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Fungal" Field="@(x => x.Fungal)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="CreatedBy" Field="@(x => x.CreatedBy)" Sortable="true" Width="10%" />
        <Column TableItem="OrganismName" Title="Created Date" Field="@(x => x.CreatedDate)" Sortable="true" Width="20%">
            <Template>
                @(context.CreatedDate.ToShortDateString())
            </Template>
        </Column>
        <Pager ShowPageNumber="true" ShowTotalCount="true" />
    </Table>
}

@if (string.IsNullOrWhiteSpace(Props.Standard.Bpstandard) ||
   string.IsNullOrWhiteSpace(Props.Group.BpgroupName))
{
    <h3> Standard or Group not selected</h3>
}
else
{
    <h3>Organisms in this group @Props.Standard.BpstandardId, Props.Group.BpGroupId</h3>
    <Table TableItem="OrganismName" DataLoader="_groupingLoader" Items="_groupingData" PageSize="5" SelectionType="selectionType" SelectedItems="_selectedOrganisms" ShowSearchBar="false">
        <Column TableItem="OrganismName" Title="Organism Id" Field="@(x => x.OrganismId)" Sortable="true" Width="10%" DefaultSortColumn="true" />
        <Column TableItem="OrganismName" Title="Organism Name" Field="@(x => x.Name)" Sortable="true" Width="20%" />
        <Column TableItem="OrganismName" Title="Family Id" Field="@(x => x.OrganismFamilyId)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="SubFamily Id" Field="@(x => x.OrganismSubfamilyId)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Genus Id" Field="@(x => x.GenusId)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Print On Code List" Field="@(x => x.PrintOnCodeList)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Remap To" Field="@(x => x.RemapTo)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Aerobic" Field="@(x => x.Aerobic)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Anaerobic" Field="@(x => x.Anaerobic)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="Fungal" Field="@(x => x.Fungal)" Sortable="true" Width="5%" />
        <Column TableItem="OrganismName" Title="CreatedBy" Field="@(x => x.CreatedBy)" Sortable="true" Width="10%" />
        <Column TableItem="OrganismName" Title="Created Date" Field="@(x => x.CreatedDate)" Sortable="true" Width="20%">
            <Template>
                @(context.CreatedDate.ToShortDateString())
            </Template>
        </Column>
        <Pager ShowPageNumber="true" ShowTotalCount="true" />
    </Table>
}

With the applicable code behind

        protected async override Task OnParametersSetAsync()
        {
            _groupingLoader = new OrgainismByGroupDataLoader(dataService, Props.Group);
            _groupingData = (await _groupingLoader.LoadDataAsync(new FilterData() { OrderBy = "OrganismId asc", Skip = 0, Top = 10 })).Records;
            _groupingExcludedLoader = new OrgainismByExcludedGroupDataLoader(dataService, Props.Group);
            _groupingExcludedData = (await _groupingExcludedLoader.LoadDataAsync(new FilterData() { OrderBy = "OrganismId asc", Skip = 0, Top = 10 })).Records;
        }

Removing the <Table> from the component lets me run without a null reference exception. If other words changing the razor to this removes the null reference exception

@using BlazorTable

<h3>Breakpoint Grouping</h3>
<h4>Standards</h4>
@BreakpointStandardCmp
<h4>Groups</h4>
@BreakpointGroupPickerCmp

@if (string.IsNullOrWhiteSpace(Props.Standard.Bpstandard) ||
  string.IsNullOrWhiteSpace(Props.Group.BpgroupName))
{
    <h3> Standard or Group not selected</h3>
}
else
{
    <h3>Organisms NOT in this group @Props.Standard.BpstandardId, @Props.Group.BpgroupId</h3>
}

@if (string.IsNullOrWhiteSpace(Props.Standard.Bpstandard) ||
  string.IsNullOrWhiteSpace(Props.Group.BpgroupName))
{
    <h3> Standard or Group not selected</h3>
}
else
{
    <h3>Organisms in this group @Props.Standard.BpstandardId, @Props.Group.BpgroupId</h3>
}

Here is the stacktrace if it is helpful:

image

Expected behavior I expect the tables to be rendered without an exception.