0

when i try to read values from a .CVS-file i get sometimes a "System.IndexOutOfRangeException - Index was outside the bounds of the array" when a cell that represents an arrayindex is empty.

my code looks like this.

 List<CustomCreatePrisInformationCommand> customCreatePrisList = new List<CustomCreatePrisInformationCommand>();
            using (var stream = new StreamReader(command.File.InputStream))
            {
                
                int rowIndex = 0;
                string line = stream.ReadLine();

                string[] ColumnsHeaders = line.Split(new char[] { ';' }, 
                    StringSplitOptions.RemoveEmptyEntries);

                line.TrimEnd(' ');
                var header = new CustomCreatePrisInformationCommand();
                header.Column1 = ColumnsHeaders[0];
                header.Column2 = ColumnsHeaders[1];
                header.Column3 = ColumnsHeaders[2];
                header.Column4 = ColumnsHeaders[3];
                header.Column5 = ColumnsHeaders[4];
                header.Column6 = ColumnsHeaders[5];
                header.Column7 = ColumnsHeaders[6];
                header.Ar2018 = System.Convert.ToInt32(ColumnsHeaders[7]);
                header.Ar2019 = System.Convert.ToInt32(ColumnsHeaders[8]);
                header.Ar2020 = System.Convert.ToInt32(ColumnsHeaders[9]);
                header.Ar2021 = System.Convert.ToInt32(ColumnsHeaders[10]);
                header.Ar2022 = System.Convert.ToInt32(ColumnsHeaders[11]);
                header.ProduktId = command.Produkt.Id;
                header.AndratAv = command.AndratAv;
                header.AndratDatum = DateTime.Now;
                header.Pristyp = command.Produkt.Pristyp;
                header.IsHeader = true;

                customCreatePrisList.Add(header);
}

the error seems to occur when a cell is empty as the file has a fixed sets of columns up to 12 in total but it also can have less, and thats when the error occurs, i was thinking if there is any solution like .ifIsnullOrEmpty() for arrays to just skip empty values to the next value? or set a default value codewise if possible like a space maybe? or is there a better way to do this?

LordWilmore
  • 2,829
  • 2
  • 25
  • 30
  • Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – derpirscher Apr 14 '23 at 10:03
  • 2
    Don't use `StringSplitOptions.RemoveEmptyEntries` – derpirscher Apr 14 '23 at 10:04

1 Answers1

2

when a cell that represents an arrayindex is empty
line.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

From the StringSplitOptions docs:

RemoveEmptyEntries Omit array elements that contain an empty string from the result.

So it will just remove all empty strings resulting in the array containing less elements (and also indexes being "shifted", i.e. you will map wrong columns). Just remove the StringSplitOptions.RemoveEmptyEntries:

string[] ColumnsHeaders = line.Split(new char[] { ';' });

But better use existing library for handling csv files like CsvHelper and don't reinvent the wheel.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132