代码中获取NullReferenceException
本文关键字:NullReferenceException 获取 代码 | 更新日期: 2023-09-27 18:09:32
这段代码有什么问题?
public partial class MainForm : Form
{
private Dictionary<String , PropertyInfo[]> types;
public MainForm()
{
//OpenAccountStruct is in the scope
types.Add("OpenAccount", new OpenAccountStruct().GetType().GetProperties());
}
}
为什么我得到NullReferenceException
您没有创建types
(您的字典)的实例。
试
types = new Dictionary<String , PropertyInfo[]>();
未初始化types
变量。使用types = new Dictionary<String , PropertyInfo[]>();
private Dictionary<String , PropertyInfo[]> types =
new Dictionary<String , PropertyInfo[]>();
显然,您的types字段没有初始化,
public partial class MainForm : Form
{
private Dictionary<String , PropertyInfo[]> types = new Dictionary<String , PropertyInfo[]>();
public MainForm()
{
//OpenAccountStruct is in the scope
types.Add("OpenAccount", new OpenAccountStruct().GetType().GetProperties());
}
}