开发者

Getting NullReferenceException in Code

开发者 https://www.devze.com 2023-04-06 11:14 出处:网络
What is wrong with this code? public partial class MainForm : Form { private Dictionary<String , PropertyInfo[]> types;

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

精彩评论

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