C# 字段从未分配给,并且其默认值始终为空(CS0649)
我一直在到处寻找答案,但我似乎无法解决我的问题。有人知道这个问题的解决方案吗?我得到以下错误: 第25行:“Champion\u Item\u List\u Downloader.MainForm.championsList”字段从未分配给,其默认值始终为空(CS0649) 第26行:“Champion\u Item\u List\u Downloader.MainForm.rolesList”字段从未分配给,其默认值始终为空(CS0649)C# 字段从未分配给,并且其默认值始终为空(CS0649),c#,C#,我一直在到处寻找答案,但我似乎无法解决我的问题。有人知道这个问题的解决方案吗?我得到以下错误: 第25行:“Champion\u Item\u List\u Downloader.MainForm.championsList”字段从未分配给,其默认值始终为空(CS0649) 第26行:“Champion\u Item\u List\u Downloader.MainForm.rolesList”字段从未分配给,其默认值始终为空(CS0649) 使用系统; 使用System.Collections
使用系统;
使用System.Collections.Generic;
使用系统图;
使用System.Windows.Forms;
使用System.IO;
Net系统;
使用系统组件模型;
名称空间冠军\u项目\u列表\u下载程序
{
///
///主窗体的描述。
///
公共部分类主窗体:窗体
{
常量字符串listFile=“Lists.txt”;
private System.Collections.Generic.List championsList;
private System.Collections.Generic.List角色列表;
公共表格(
{
//
//Windows窗体设计器支持需要InitializeComponent()调用。
//
初始化组件();
加载列表(列表文件);
}
公共无效加载列表(字符串文件){
试一试{
使用(StreamReader r=新StreamReader(文件))
{
弦线;
bool-isChamp=false;
而((line=r.ReadLine())!=null)
{
如果(行==@“[Champions]”){
isChamp=true;
}
if(line!=@“[Champions]”和&line!=@“[Types]”和&line!=”)
{
if(isChamp==true){
championsList.Add(行);
}否则{
角色列表。添加(行);
}
}
}
}
}捕获(例外){
}
}
公共void loadStringList(字符串文件,列表){
试一试{
使用(StreamReader r=新StreamReader(文件))
{
弦线;
而((line=r.ReadLine())!=null)
{
列表。添加(行);
}
}
}捕获(例外){
}
}
void Btn_DownloadClick(对象发送者,事件参数e)
{
WebClient WebClient=新的WebClient();
progressBar.max=championsList.Count*rolesList.Count;
整数计数=0;
progressBar.Value=0;
字符串fileName=“”;
字符串url=“”;
字符串路径=”;
foreach(championsList中的字符串c)
{
foreach(角色列表中的字符串r)
{
试一试{
fileName=c+@“\uu+@”\u scrap.json”;
url=@“http://www.lolflavor.com/champions/“+c+@”/推荐/“+文件名;
路径=@“冠军\”+c+@“\推荐\”;
CreateDirectory(路径);
下载文件(新的Uri(url),路径+文件名);
计数++;
progressBar.Value=计数;
}捕获(例外){
}
}
}
progressBar.Value=progressBar.Maximum;
MessageBox.Show(“下载完成!\n”+count.ToString()+“已成功下载的项目列表”);
}
void MainFormLoad(对象发送方,System.EventArgs e)
{
抛出新的NotImplementedException();
}
}
}
从未调用过championsList=new System.Collections.Generic.List()
因此,它永远不会初始化从来没有调用
championsList=new System.Collections.Generic.List()
因此它永远不会初始化您还没有在任何地方初始化列表 尝试在声明它们的位置初始化它们:
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 = new System.Collections.Generic.List<string>();
private System.Collections.Generic.List<string> rolesList = new System.Collections.Generic.List<string>();
您没有初始化消息中提到的字段。你应该这样做。你从不初始化你的列表。你所做的一切都要申报。这意味着您已经留出了在程序中存储列表的空间。但是,你从来没有将列表对象放入该槽中。这个问题是真实的,所以+1是必要的。请不要阻止初学者提问。您没有初始化消息中提到的字段。你应该这样做。你从不初始化你的列表。你所做的一切都要申报。这意味着您已经留出了在程序中存储列表的空间。但是,你从来没有将列表对象放入该槽中。这个问题是真实的,所以+1是必要的。请不要劝阻初学者提问。你应该仔细阅读。使用backtick`标记内联代码块。@Aron,感谢您的修复,在以后的回答中会记住这一点。您应该仔细阅读SO标记。使用backtick`标记内联代码块。@Aron,感谢您的修复,将在以后的回答中记住这一点。-1分配已完成。尚未初始化。-1个分配已完成。初始化尚未完成。
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.");
}
private List<string> championsList = new List<string>();
private List<string> rolesList = new List<string>();