Blazor server-side, razor page: Object reference not set to an instance of an object

1

I am using .NET 5 version 5.0.100-rc.1.20452.10 , ASP.NET Core Blazor server-side.

I mimic this example https://github.com/DevExpress-Examples/blazor-server-dxdatagrid-export/blob/19.2.2%2B/CS/DxDataGridExportingWithReports/Pages/Index.razor#L22

My error

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=acc133blazor
  StackTrace:
   at acc133blazor.Pages.DeclaredData.Account.AccountsPage.<OnInitializedAsync>d__10.MoveNext() in D:\acc133blazor\acc133blazor\Pages\DeclaredData\Account\AccountsPage.razor:line 144

@foreach (Tuple<string, string> urlInfo in ExportUrlInfo)
{
    <a href="@urlInfo.Item1" download target="_blank">@urlInfo.Item2</a>
}

    <br />
    <br />
}

@code {
    private readonly ObservableCollection<Tuple<string, string>> exportUrlInfo = new ObservableCollection<Tuple<string, string>>();

    protected Task<LoadResult> LoadOrderData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken) {
        string baseUri = NavigationManager.BaseUri.ToString();
        exportUrlInfo.Clear();
        exportUrlInfo.Add(Tuple.Create(options.ConvertToGetRequestUri(baseUri + "exportPdf"), "Export PDF"));
        exportUrlInfo.Add(Tuple.Create(options.ConvertToGetRequestUri(baseUri + "exportXlsx"), "Export XLSX"));
        exportUrlInfo.Add(Tuple.Create(options.ConvertToGetRequestUri(baseUri + "exportDocx"), "Export DOCX"));
        return Task.FromResult(DataSourceLoader.Load(AccountList, options));
    }

    public ObservableCollection<Tuple<string, string>> ExportUrlInfo { get; set; }

//...
    protected override async Task OnInitializedAsync()
    {
        AccountList = await Controller.GetAccountAsync();
        ListOfAccountTypes = new List<ComboBoxItem>()
        {
            new ComboBoxItem(0,"Dư Nợ"),
            new ComboBoxItem(1,"Dư Có"),
            new ComboBoxItem(2,"Lưỡng tính")
        };
        ExportUrlInfo.CollectionChanged += (s, e) =>
        {
            InvokeAsync(StateHasChanged);
        };
    }

enter image description here

How to fix error?

c#
asp.net-core
razor
blazor
blazor-server-side
asked on Stack Overflow Sep 19, 2020 by Do Nhu Vy • edited Sep 19, 2020 by Do Nhu Vy

1 Answer

1

The problem is here ExportUrlInfo.CollectionChanged. The ExportUrlInfo is not yet initialized.

The reference types need to get an initial value:

The following declaration does not mean that it will have a value

public ObservableCollection<Tuple<string, string>> ExportUrlInfo { get; set; }

You have two options:

  1. Change the property to use the backing variable:
public ObservableCollection<Tuple<string, string>> ExportUrlInfo { get { return exportUrlInfo; } set { exportUrlInfo = value; } }
  1. Change the reference to the private member
        this.exportUrlInfo.CollectionChanged += (s, e) =>
        {
            InvokeAsync(StateHasChanged);
        };
answered on Stack Overflow Sep 19, 2020 by Athanasios Kataras • edited Sep 19, 2020 by Athanasios Kataras

User contributions licensed under CC BY-SA 3.0