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
}
}
}
I want to get every 3rd value from array content, and put it into array id. This gives a ‘NullReferenceException was unhandled’ error and suggests I use ‘new’, but it is not a type or namespace. What should I do here?
They are both string arrays, and the error occurs on the first run so I do not think it is related to exceeding the bounds.
This line of code:
is actually creating a
nullreference.When you declare an array, you have to explicitly create it, specifying the size.
In your example, you have two alternatives
The first option:
The second option:
The first option would perform better, as you are only allocating one object.
Admittedly, I tend to disregard performance and use the second option, because it takes less brainpower to code.