字段永远不会分配给,并且将始终具有其默认值null(CS0649)

时间:2014-05-29 04:21:41

标签: c#

我到处寻找答案,我似乎无法解决我的问题。有人知道解决方案吗?我收到以下错误:

第25行:Field' Champion_Item_List_Downloader.MainForm.championsList'永远不会分配给,并且将始终将其默认值设为null(CS0649)

第26行:Field' Champion_Item_List_Downloader.MainForm.rolesList'永远不会分配给,并且将始终将其默认值设为null(CS0649)

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.ComponentModel;

namespace Champion_Item_List_Downloader
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        const string listFile = "Lists.txt";
        private System.Collections.Generic.List<string> championsList;
        private System.Collections.Generic.List<string> rolesList;

        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            loadList(listFile);
        }

        public void loadList(string file){
            try {
                using (StreamReader r = new StreamReader(file))
                {
                    string line;
                    bool isChamp = false;
                    while ((line = r.ReadLine()) != null)
                    {
                        if (line == @"[Champions]") {
                            isChamp = true;
                        }

                        if(line != @"[Champions]" && line != @"[Types]" && line != "")
                        {
                            if(isChamp == true){
                                championsList.Add(line);
                            } else {
                                rolesList.Add(line);
                            }
                        }
                    }
                }
            } catch (Exception) {
            }
        }

        public void loadStringList(string file, List<string> list){
            try {
                using (StreamReader r = new StreamReader(file))
                {
                    string line;
                    while ((line = r.ReadLine()) != null)
                    {
                        list.Add(line);
                    }
                }
            } catch (Exception) {
            }
        }

        void Btn_DownloadClick(object sender, EventArgs e)
        {
            WebClient webClient = new WebClient();

            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)
                {
                    try {
                        fileName = c + @"_" + r + @"_scrape.json";
                        url = @"http://www.lolflavor.com/champions/" + c + @"/Recommended/" + fileName;
                        path = @"Champions\" + c + @"\Recommended\";
                        Directory.CreateDirectory(path);
                        webClient.DownloadFile(new Uri(url), path + fileName);
                        count++;
                        progressBar.Value = count;

                    } catch (Exception) {
                    }
                }
            }

            progressBar.Value = progressBar.Maximum;

            MessageBox.Show("Download completed!\n" + count.ToString() + " item lists successfully downloaded.");
        }

        void MainFormLoad(object sender, System.EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您尚未在任何地方初始化您的列表。

尝试在声明它们的位置初始化它们:

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

或在构造函数中:

    private System.Collections.Generic.List<string> championsList;
    private System.Collections.Generic.List<string> 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<string>();
        rolesList = new System.Collections.Generic.List<string>();
        loadList(listFile);
    }

或者在他们需要的时候懒洋洋地说:

    private System.Collections.Generic.List<string> championsList;
    private System.Collections.Generic.List<string> rolesList;

    public void loadList(string file){
        if (championsList == null) championsList = new System.Collections.Generic.List<string>();
        if (rolesList == null) rolesList = new System.Collections.Generic.List<string>();
        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<string>();
        if (rolesList == null) rolesList = new System.Collections.Generic.List<string>();

        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。因为您已经声明了using System.Collections.Generic,所以您可以更简单地将其写为:

    private List<string> championsList = new List<string>();
    private List<string> rolesList = new List<string>();

答案 1 :(得分:1)

永远不会打电话给championsList = new System.Collections.Generic.List<string>()

因此它从未初始化