0

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

4
  • p.s.- Yes, I know there are many libraries that allow me to convert data to an array in C#. I'm just doing this exercise for self-learning purposes. Commented Sep 2, 2015 at 0:51
  • 1
    Debug it by stepping through the code. Commented Sep 2, 2015 at 0:55
  • how do you give values to row_count and column_count? Commented Sep 2, 2015 at 0:56
  • Your readline() should be on row loop i believe Commented Sep 2, 2015 at 0:57

1 Answer 1

2

First Mistake

You are looping the row_count

You have to loop the row_index

Then

Your readline() statement is inside the second loop..

So each time when it executes the readline() statement, it jumps to the next line. While here in your for loop you want to loop until your row_count, so that causes the file to be read more running out of lines and causes the exception

Do like

for(int row_index = 0; row_index < row_count; row_index++){//<= You were using row_count++
    var line = reader.ReadLine();
    var values = line.Split(',');
    for(int column_index = 0; column_index < column_count; column_index++){
        // seem to be having problem here. It compiles but returns an unhandled exception
        file_data[row_index, column_index] = values[column_index];

    }
}

Note : If you dont wish to hardcode the number of rows, you can limit the loop using EndOfStream

for(int row_index = 0; !reader.EndOfStream; row_index++)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.