我们的程序崩溃了,现在我们无法重现.我试图放入一些代码来防止它再次发生,但我对堆栈跟踪感到困惑.
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Object.GetType()
at Project.ViewModel.MainVM.<CreateCommands>b__8(Object a)
at System.Windows.Controls.Button.OnClick()
Run Code Online (Sandbox Code Playgroud)
- 我已经减少了堆栈跟踪,因为它只是进入一系列系统代码,这与点击的按钮有关. -
我设法推断它指向我的CreateCommands方法第8行的匿名委托.
this.sectionCommand = new DelegateCommand(a =>
{
this.OnSectionParameterChanged((Sections)a);
}, p => this.IsSectionCommandExecutable);
Run Code Online (Sandbox Code Playgroud)
我在这里看过类似的帖子,但是OP明确地调用了GetType.我假设转换调用get类型,但无法重现问题我无法看到什么是null.
所以我的问题是:对于这个堆栈跟踪导致空引用,空对象的'a'变量是什么?(所以我会写一些像)
if (a != null)
{
this.OnSectionParameterChanged((Sections)a);
}
Run Code Online (Sandbox Code Playgroud)
或者是从'a'到'sections'的转换导致空对象?(所以我应该写点东西)
if (a is Sections)
{
this.OnSectionParameterChanged((Sections)a);
}
Run Code Online (Sandbox Code Playgroud)
这里要求的是OnSectionParameterChanged
private void OnSectionParameterChanged(Sections parameter)
{
this.SelectedSection = parameter;
this.RaisePropertyChanged(() => this.SelectedSection);
this.LoadSettingsPanel();
}
Run Code Online (Sandbox Code Playgroud)
进一步说,它调用LoadSettingsPanel
private void LoadSettingsPanel()
{
if (sectionVMs == null)
return;
// Get section
SectionViewModel = …Run Code Online (Sandbox Code Playgroud)