wpf c#错误对象引用未设置为对象的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37260518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# error Object reference not set to an instance of an object
提问by Satish
I have a window with a datagrid and a chart. The data grid has 5 rows of data and the chart graphs the data of the row based on the selected row. Right now it only graphs the first row and doesn't update the graph when I select a different row. This code is in a class called ShellViewModel.cs:
我有一个带有数据网格和图表的窗口。数据网格有 5 行数据,图表根据所选行绘制该行的数据。现在它只绘制第一行的图形,当我选择不同的行时不会更新图形。此代码位于名为 ShellViewModel.cs 的类中:
public void EditLoadForecastViewModel()
{
Shell sh;
Timer timer = new Timer(500);
timer.Elapsed += new ElapsedEventHandler((s, e) =>
{
if (updateGraph)
{
sh.Dispatcher.BeginInvoke(new Action(() => UpdateLoadChart()), null);
updateGraph = false;
}
});
timer.Start()
}
This function is called in a window called Shell.xaml like this
此函数在名为 Shell.xaml 的窗口中调用,如下所示
private void btnResults_Click(object sender, RoutedEventArgs e)
{
vm.ReadLoadForecastFile(false,false);
vm.EditLoadForecastViewModel();
}
When the button is pressed i get an error pointing at this line:
当按下按钮时,我收到一个指向这一行的错误:
sh.Dispatcher.BeginInvoke(new Action(() => UpdateLoadChart()), null);
The error message is
错误信息是
NullReferenceException was unhandled by user code : Object reference not set to an instance of an object
用户代码未处理 NullReferenceException : 未将对象引用设置为对象的实例
回答by sammarcow
Instantiate a new instance of the Shell object sh.
实例化 Shell 对象 sh 的新实例。
Shell sh = new Shell();

