gpt4 book ai didi

c# - Blazor 服务器端, Razor 页面 : Object reference not set to an instance of an object

转载 作者:行者123 更新时间:2023-12-04 08:53:39 24 4
gpt4 key购买 nike

我正在使用 .NET 5 版本 5.0.100-rc.1.20452.10 , ASP.NET Core Blazor 服务器端。
我模仿这个例子https://github.com/DevExpress-Examples/blazor-server-dxdatagrid-export/blob/19.2.2%2B/CS/DxDataGridExportingWithReports/Pages/Index.razor#L22
我的错误

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
如何修复错误?

最佳答案

问题出在这里ExportUrlInfo.CollectionChanged . ExportUrlInfo尚未初始化。
引用类型需要获取一个初始值:
以下声明并不意味着它将具有值

public ObservableCollection<Tuple<string, string>> ExportUrlInfo { get; set; }
你有两个选择:
  • 更改属性以使用支持变量:
  • public ObservableCollection<Tuple<string, string>> ExportUrlInfo { get { return exportUrlInfo; } set { exportUrlInfo = value; } }
  • 更改对私有(private)成员的引用
  •         this.exportUrlInfo.CollectionChanged += (s, e) =>
    {
    InvokeAsync(StateHasChanged);
    };

    关于c# - Blazor 服务器端, Razor 页面 : Object reference not set to an instance of an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63965382/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com