gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:52:31 26 4
gpt4 key购买 nike

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

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

最佳答案

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

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

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

关于c# - 字段永远不会分配给,并且将始终具有其默认值 null (CS0649),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23925643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com