Field is never assigned to, and will always have its default value null (CS0649)

前端 未结 2 902
执念已碎
执念已碎 2020-12-12 01:34

I\'ve been searching for answers everywhere and I can\'t seem to solve mine. Anyone know a solution for this? I\'m getting the following errors:

Line 25: Field \'Cha

2条回答
  •  天命终不由人
    2020-12-12 02:16

    You have not initialized your lists anywhere.

    Try initializing them where they are declared:

        private System.Collections.Generic.List championsList = new System.Collections.Generic.List();
        private System.Collections.Generic.List rolesList = new System.Collections.Generic.List();
    

    Or within the constructor:

        private System.Collections.Generic.List championsList;
        private System.Collections.Generic.List rolesList;
    
        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            // loadList uses these lists so they should be initialized before the call to loadList to avoid a NullReferenceException.
            championsList = new System.Collections.Generic.List();
            rolesList = new System.Collections.Generic.List();
            loadList(listFile);
        }
    

    Or lazily at the time they are needed:

        private System.Collections.Generic.List championsList;
        private System.Collections.Generic.List rolesList;
    
        public void loadList(string file){
            if (championsList == null) championsList = new System.Collections.Generic.List();
            if (rolesList == null) rolesList = new System.Collections.Generic.List();
            try {
                using (StreamReader r = new StreamReader(file))
                {
                   ...
                }
            } catch (Exception) {
            }
        }
    
        void Btn_DownloadClick(object sender, EventArgs e)
        {
            WebClient webClient = new WebClient();
            if (championsList == null) championsList = new System.Collections.Generic.List();
            if (rolesList == null) rolesList = new System.Collections.Generic.List();
    
            progressBar.Maximum = championsList.Count * rolesList.Count;
            int count = 0;
            progressBar.Value = 0;
    
            string fileName = "";
            string url = "";
            string path = "";
    
            foreach (string c in championsList)
            {
                foreach (string r in rolesList)
                {
                    ...
                }
            }
    
            progressBar.Value = progressBar.Maximum;
    
            MessageBox.Show("Download completed!\n" + count.ToString() + " item lists successfully downloaded.");
        }
    

    P.S. because you already have using System.Collections.Generic declared, you could write it more simply as:

        private List championsList = new List();
        private List rolesList = new List();
    

提交回复
热议问题