Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

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

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 'Champion_Item_List_Downloader.MainForm.championsList' is never assigned to, and will always have its default value null (CS0649)

Line 26: Field 'Champion_Item_List_Downloader.MainForm.rolesList' is never assigned to, and will always have its default value 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!
" + count.ToString() + " item lists successfully downloaded.");
        }

        void MainFormLoad(object sender, System.EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have not initialized your lists anywhere.

Try initializing them where they are declared:

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

Or within the constructor:

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

Or lazily at the time they are needed:

    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!
" + 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<string> championsList = new List<string>();
    private List<string> rolesList = new List<string>();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...