0

I have the following function reading CSV files from a FTP site:

public void FromCSV(string csvdata)
        {

        string[] data = csvdata.Split(",", StringSplitOptions.None);
            StudentId = data[0];
            FirstName = data[1];
            LastName = data[2];
            DateOfBirth = data[3];
            ImageData = data[4];         
    }

At a certain point, I receive a empty "ImageData" and I it gives me the error bellow:

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

The only thing I did that brought it closer to working was:

public void FromCSV(string csvdata)
        {
            string[] data = csvdata.Split(",", StringSplitOptions.None);
            if ((data[0] != null && data[1] != null && data[2] != null && data[3] != null && data[4] != null) && ((data[0].Length != 0 && data[1].Length != 0 && data[2].Length != 0 && data[3].Length != 0 && data[4].Length != 0)))
            {                
                StudentId = data[0];
                FirstName = data[1];
                LastName = data[2];
                DateOfBirth = data[3];
                ImageData = data[4];
            }
            else
            {
                Console.WriteLine("CSV with incomplete data");
            }

        }

The problem is that it is still allowing empty strings to go to the data[0:4], hence crashing the program. What am I doing wrong? How to create a test that check for empty values in the array and how to output a message when a empty space is found?

  • CSV allows ",,,,," as a valid input. You have to elaborate on what you actually want (No empty, no invalid path...etc). Also using [CSVHelper](https://joshclose.github.io/CsvHelper/) is a lot easier than parsing yourself. – Louis Go Mar 24 '20 at 02:37
  • 1
    The issue you describe is nothing to do with `null`. If you split the following string, how many items do you get?: `a,b,c,d`. 4, right? So `Split` gives you an array of 4 items, since that's all you have. There isn't a 5th null item - it simply doesn't exist. Note that `Split` will never give you any `null` items. – ProgrammingLlama Mar 24 '20 at 02:39
  • 1
    By your description "The problem is that it is still allowing empty strings to go to the data[0:4], hence crashing the program.", I'll assume the problem is at loading image. Do not loading invalid path or data into image. Validate your source and then load it. Never take a faithful leap on input. – Louis Go Mar 24 '20 at 02:40
  • The data crashing the loop is: ```StudentId,FirstName,LastName,DateOfBirth,ImageData 200222222,Vrus,Jarminder,1996/06/15``` Could it be the lack of a coma after the date? Expects 5 records and receive 4, so out of bounds? – Bruno Simões Mar 24 '20 at 02:53
  • That's exactly the problem. You should probably check `data.Length` in order to verify if data exists for a given index (in your example, `data.Length` will be 4 because there are only 4 items). – ProgrammingLlama Mar 24 '20 at 02:55
  • The thing is that one of my tasks on thsi assignment I will have to add new columns and headers to my own record on thsi FTP site. Tricky, huh? Once the rest of my cologues also do so, they some arrays will also increase to 6. – Bruno Simões Mar 24 '20 at 02:59
  • Honestly, I'd recommend using a dedicated CSV library such as CsvHelper.IO. CSV files aren't particularly consistent, and in some instances you even have newlines within a single record (within a string cell, denoted by " at the beginning and end). CsvHelper.IO can deal with a lot of these issues. Unless this is a school assignment to teach you to use Split, etc. then I'd recommend using a library to help you. – ProgrammingLlama Mar 24 '20 at 03:00
  • Sure, its a bit tedious, but you could check the array length before assigning the value. Example: `StudentId = data.Length > 0 ? data[0] : string.Empty;` and `FirstName = data.Length > 1 ? data[1] : string.Empty;`... – Barns Mar 24 '20 at 03:14
  • A actually found a work around: ``` public void FromCSV(string csvdata) { string[] data = csvdata.Split(",", StringSplitOptions.None); var dataSize = data.Length; if (dataSize.Equals(5) || dataSize.Equals(6)) ``` That did the trick. – Bruno Simões Mar 24 '20 at 04:37
  • @Jhon I would love if my professor would allow that. Seems like he wants us hard coding the solution. – Bruno Simões Mar 24 '20 at 04:39

0 Answers0