【发布时间】:2014-05-29 04:21:41
【问题描述】:
我一直在到处寻找答案,但我似乎无法解决我的问题。有谁知道这个的解决方案?我收到以下错误:
第 25 行:字段 'Champion_Item_List_Downloader.MainForm.championsList' 从未分配给,并且始终具有其默认值 null (CS0649)
第 26 行:字段 '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();
}
}
}
【问题讨论】:
-
您没有初始化消息中提到的字段。你应该这样做。
-
你永远不会初始化你的列表。你所做的一切都声明了它们。这意味着您有空间在程序中存储列表。但是,您永远不会将列表对象放入该插槽中。
-
问题是真实的,所以+1是必要的。请不要阻止初学者提问。
标签: c#