Object reference not set to an instance of an object.

I get this error

blazor.server.js:1 [2022-01-14T07:52:44.127Z] Error: System.NullReferenceException: Object reference not set to an instance of an object.

   at Syncfusion.Blazor.Grids.GridAggregate.OnInitializedAsync()

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

  when trying to Sum Value


 <SfGrid DataSource="@Patient" AllowPaging="true" AllowExcelExport="true" AllowFiltering="true">

         <GridPageSettings PageCount="5" PageSizes="true"></GridPageSettings>

                <GridColumns>

                    <GridColumn Field=@nameof(PatientReadDTO.Id) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120" Visible=false></GridColumn>

                    <GridColumn Field=@nameof(PatientReadDTO.Name) HeaderText="Patient Nam" Width="150"></GridColumn>

      <GridColumn Field=@nameof(PatientReadDTO.DoctorName) HeaderText="Doctor Name" Width="150"></GridColumn>

      <GridColumn Field=@nameof(PatientReadDTO.Opdtype) HeaderText="OPD Type"></GridColumn>


                </GridColumns>

             <GridAggregate>

                        <GridAggregateColumns>

                            <GridAggregateColumn Field="@nameof(PatientReadDTO.Opdfess)" Type="AggregateType.Sum" Format="C2">

                                <FooterTemplate>

                                    @{ var SumValue = (context as AggregateTemplateContext);

                                    <div>

                                        Sum: @SumValue.Sum

                                    </div>

                                    }

                                </FooterTemplate>

                            </GridAggregateColumn>

                        </GridAggregateColumns>

                    </GridAggregate>

            </SfGrid>




@Code{

    private List<PatientReadDTO> Patient;

    private Response<List<PatientReadDTO>> response = new Response<List<PatientReadDTO>> { Success = true };

    protected override async Task OnInitializedAsync()

    {

        response = await pService.GetPatients();

        if(response.Success)

        {

            Patient = response.Data.Take(5).ToList();;

        }


    }


}

I do this like from this demo https://blazor.syncfusion.com/demos/datagrid/aggregate?theme=bootstrap5

and 1 more issue i already define page value 5 or 10 from the grid but its show all the records at starting

Please review


5 Replies

RS Renjith Singh Rajendran Syncfusion Team January 17, 2022 08:46 AM UTC

Hi Vikas, 
 
Greetings from Syncfusion support. 
 
We have analyzed the shared codes and we could see that you have missed the enclosing GridAggregates component has caused the reported problem. So we suggest you to ensure to add the below highlighted code to overcome the reported problem. 
References :  
 
<SfGrid DataSource="@Patient" AllowPaging="true" AllowExcelExport="true" AllowFiltering="true"> 
               <GridColumns> 
                    ... 
               </GridColumns> 
                <GridAggregates> 
                    <GridAggregate> 
                        <GridAggregateColumns> 
                            <GridAggregateColumn Field="@nameof(PatientReadDTO.Opdfess)" Type="AggregateType.Sum" Format="C2"> 
                                <FooterTemplate> 
                                    @{ var SumValue = (context as AggregateTemplateContext); 
                                    <div> 
                                        Sum: @SumValue.Sum 
                                    </div> 
                                    } 
                                </FooterTemplate> 
                            </GridAggregateColumn> 
                        </GridAggregateColumns> 
                    </GridAggregate> 
                 </GridAggregates> 
            </SfGrid> 
 
 
Query 2 : 1 more issue i already define page value 5 or 10 from the grid but its show all the records at starting 
We suspect that you would like to limit the records available in a particular Grid page. If so, then we suggest you to use the PageSize property of Grid instead of using PageCount. Please refer and use the codes below, 
 
 
<GridPageSettings PageSize="5" PageSizes="true"></GridPageSettings> 
 
 
 
Please get back to us if you need further assistance. 
 
Regards, 
Renjith R 



VI vikas January 17, 2022 01:47 PM UTC

Thankyou Sir



RS Renjith Singh Rajendran Syncfusion Team January 18, 2022 05:20 AM UTC

Hi Vikas, 
 
Thanks for your update. Please get back to us if you need further assistance. 
 
Regards, 
Renjith R 



MA macronbell October 4, 2022 04:48 AM UTC

An Object is an instance of a Class , it is stored some where in memory. A reference is what is used to describe the pointer to the memory location where the Object resides. The message "object reference not set to an instance of an object" means that you are referring to an object the does not exist or was deleted or cleaned up. It's usually better to avoid a NullReferenceException than to handle it after it occurs. To prevent the error, objects that could be null should be tested for null before being used.


if (mClass != null)

{

  // Go ahead and use mClass

  mClass.property = ...

}

else

{

  // Attempting to use mClass here will result in NullReferenceException

}


A NullReferenceException typically reflects developer error and is thrown in the following scenarios:


  • Forgotten to instantiate a reference type.
  • Forgotten to dimension an array before initializing it.
  • Is thrown by a method that is passed null.
  • Get a null return value from a method, then call a method on the returned type.
  • Using an expression to retrieve a value and, although checking whether the value is null.
  • Enumerating the elements of an array that contains reference types, and attempt to process one of the elements.





MS Monisha Saravanan Syncfusion Team October 5, 2022 01:29 PM UTC

Hi Macronbell,


Thanks for the detailed explanation.


Regards,

Monisha


Loader.
Up arrow icon