C# 神秘系统.Object.GetType()NullReferenceException
我们的程序崩溃了,现在无法重现。我试图输入一些代码来防止它再次发生,但我对堆栈跟踪感到困惑C# 神秘系统.Object.GetType()NullReferenceException,c#,casting,C#,Casting,我们的程序崩溃了,现在无法重现。我试图输入一些代码来防止它再次发生,但我对堆栈跟踪感到困惑 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.
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()
我在这里看到过类似的帖子,但是OP明确地调用了GetType。我假设cast调用得到type,但是如果不能重现问题,我就看不到什么是null
所以我的问题是:对于这个堆栈跟踪导致空引用,“a”变量是空对象吗?(所以我会写一些类似的东西)
或者从“a”到“sections”的强制转换是否导致空对象?(所以我应该写一些类似的东西)
根据此处的要求,OnSectionParameter已更改
private void OnSectionParameterChanged(Sections parameter)
{
this.SelectedSection = parameter;
this.RaisePropertyChanged(() => this.SelectedSection);
this.LoadSettingsPanel();
}
此外,它还调用LoadSettingsPanel
private void LoadSettingsPanel()
{
if (sectionVMs == null)
return;
// Get section
SectionViewModel = sectionVMs.SingleOrDefault(s.SectionName == SelectedSection);
this.IsSelectedSectionEnabled = this.Config.GetIsSectionEnabled(this.SelectedSection);
this.RaisePropertyChanged(() => this.IsSelectedSectionEnabled);
// Set advanced
AdvancedViewModel = this.SectionViewModel;
if (AdvancedViewModel != null)
HasAdvanced = AdvancedViewModel.HasAdvanced;
}
我所描述的问题实际上不是真正的问题。我在另一个网站上读到,堆栈跟踪的
b_uu8
部分意味着问题出现在CreateCommands
方法的第8行。这与一个匿名委托完全一致,我可以看到它如何匹配bug报告中的行为
实际上,我通过使用IL Dasm找到了问题的解决方案(可以在
\程序文件(x86)\Microsoft SDK\Windows\v7.0A\Bin
打开运行的EXE,找到了.net认为的
b_uu8
实际上是什么。这原来是另一个匿名委托,它显式地调用了.GetType()
因此,一旦我了解了b_uu8
的实际含义,问题其实很容易解决。更有趣的问题是:为什么虽然没有调用GetType,但它仍在堆栈上?发布OnSectionParameter的主体。它可能已内联并被称为GetType。此外,GetType本身如何会因NRE而崩溃?这是一个问题吗CLR错误?您是否尝试过调试?在之前放置一个断点。OnSectionParameterChanged()
并亲自查看a
的值,然后使用即时窗口查看(节)a)
的值是否只是一次性错误,不可重复?异常可能是运行应用程序的计算机上的另一个问题的症状。请显示更多堆栈跟踪。根据CoreClr,GetType是一个内在类型,如果使用null引用调用它,它将抛出NRE(ObjectNative::GetClass
)。通常,C#调用带有空检查的方法,这就是为什么我想知道这怎么可能发生的原因。我们需要查看OnSectionParameter是否已更改。
if (a is Sections)
{
this.OnSectionParameterChanged((Sections)a);
}
private void OnSectionParameterChanged(Sections parameter)
{
this.SelectedSection = parameter;
this.RaisePropertyChanged(() => this.SelectedSection);
this.LoadSettingsPanel();
}
private void LoadSettingsPanel()
{
if (sectionVMs == null)
return;
// Get section
SectionViewModel = sectionVMs.SingleOrDefault(s.SectionName == SelectedSection);
this.IsSelectedSectionEnabled = this.Config.GetIsSectionEnabled(this.SelectedSection);
this.RaisePropertyChanged(() => this.IsSelectedSectionEnabled);
// Set advanced
AdvancedViewModel = this.SectionViewModel;
if (AdvancedViewModel != null)
HasAdvanced = AdvancedViewModel.HasAdvanced;
}