Getting NullReferenceException in Code
What is wrong with this code?
public partial class MainForm : Form
{
private Dictionary<String , PropertyInfo[]> types;
public MainForm()
{
//OpenAccountStruct is in the scope
types.Add("OpenAccount", new OpenAccountStruct().GetType().GetProperties());
}
}
Why am I getting NullR开发者_运维问答eferenceException
?
You didn't make an instance of types
(your Dictionary).
try
types = new Dictionary<String , PropertyInfo[]>();
The types
variable is not initialized.
Use types = new Dictionary<String , PropertyInfo[]>();
private Dictionary<String , PropertyInfo[]> types =
new Dictionary<String , PropertyInfo[]>();
Obviously, your types field is not initialized,
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());
}
}
精彩评论