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

本文关键字:默认值 null CS0649 永远 分配 字段 | 更新日期: 2023-09-27 18:05:35

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

第25行:字段' champion_item_list_download . mainform . 'championsList'永远不会被分配给,并且将始终具有其默认值null (CS0649)

第26行:字段' champion_item_list_download . 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();
        }
    }
}

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

您还没有初始化您的列表。

尝试在声明它们的地方初始化它们:

    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.");
    }

注:因为已经声明了using System.Collections.Generic,所以可以更简单地写为:

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

从来没有调用过championsList = new System.Collections.Generic.List<string>()

因此它从未初始化