For the sake of learning C# more, I am attempting to make my own (albeit rudimentary) CSV reader, to take a CSV file where the first row has descriptions, and then the remaining rows hold numerical data.
I'm able to find the number of rows and elements. Since I know these two numbers, I just declare an array:
string [,] file_data = new string[row_count, column_count];
The trouble shows up when I try to read the values from the CSV file and store them into the two dimensional array:
var reader = new StreamReader(File.OpenRead(user_input_file));
for(int row_index = 0; row_index < row_count; row_count++){
for(int column_index = 0; column_index < column_count; column_index++){
var line = reader.ReadLine();
var values = line.Split(',');
// seem to be having problem here.
// It compiles but returns an unhandled exception
file_data[row_index, column_index] = values[column_index];
}
}
When I go to compile the code, I have no issues; when I run the code in the terminal, however, I get the following error:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at ReadCSV.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at ReadCSV.Main (System.String[] args) [0x00000] in <filename unknown>:0