public partial class Form1 : Form
{
string[] id;
private void button_Click(object sender, EventArgs e)
{
char[] delimiters = { ',', '\r', '\n' };
string[] content = File.ReadAllText(CSV_File).Split(delimiters);
int x = content.GetUpperBounds(0)
int z = 0;
int i - 0;
for (i = 0; i <= x / 3; i++)
{
z = (i * 3);
id[i] = content[z]; // this line gives the error
}
}
}
我想从数组内容中获取每第 3 个值,并将其放入数组 id 中。这给出了“NullReferenceException 未处理”错误并建议我使用“new”,但它不是类型或 namespace 。我应该在这里做什么?
都是字符串数组,第一次运行就报错,我觉得跟越界没有关系。
最佳答案
您需要在for
循环之前初始化id
数组:
id = new string[x/3];
关于c# - 我该如何处理这个 NullReferenceException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8958093/