开发者

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());
        }
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜