-1

I'm working on a bit of code for school but I keep getting an ArgumentOutOfRangeException

With this code I'm trying to read some data from a .csv file and if it equals the name of the image I want it to remove it from the .csv file whilst keeping the structure intact.

public void checkPair(Image card1, Image card2)
    {
        this.Image1 = card1;
        this.Image2 = card2;

        if (Convert.ToString(card1.Source) == Convert.ToString(card2.Source) && (card1 != card2))
        {
            getPoint(card1, card2);



            string path = @"Save1.csv";

            var reader = new StreamReader(File.OpenRead(path));
            var data = new List<List<string>>();

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(';');

                data.Add(new List<String> { values[0], values[1]
                    });
            }
            reader.Close();

            string delimiter = ";";

            for (int i = 1; i < 5; i++)
            {
                for (int x = 0; x < 4; x++)
                {
                    if (data[i][x] == Convert.ToString(card1.Source))
                    {
                        data[i][x] = null;
                    }
                }
            }

            File.WriteAllText(path, data[0][0] + delimiter + data[0][1] + Environment.NewLine + data[1][0] + delimiter + data[1][1] + delimiter + data[1][2] + delimiter + data[1][3] + Environment.NewLine + data[2][0] + delimiter + data[2][1] + delimiter + data[2][2] + delimiter + data[2][3] + Environment.NewLine + data[3][0] + delimiter + data[3][1] + delimiter + data[3][2] + delimiter + data[3][3] + Environment.NewLine + data[4][0] + delimiter + data[4][1] + delimiter + data[4][2] + delimiter + data[4][3] + Environment.NewLine + "ready");

I have no idea why I get this error and how to fix it

bencher
  • 21
  • 4
  • I'd start comparing `data.Add(new List { values[0], values[1] });` with `for (int x = 0; x < 4; x++)` - I'm not sure how accessing indices 2 or 3 will work when you only add 2 items to the list. If you're getting the error on the `data.Add` line, then you've reached a line in your file that doesn't result in 2 items after the split. I recommend using the [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to analyse your variables. – ProgrammingLlama Oct 28 '18 at 13:41
  • Possible duplicate of [ArgumentOutOfRangeException on initialized List](https://stackoverflow.com/questions/4236629/argumentoutofrangeexception-on-initialized-list) – ProgrammingLlama Oct 28 '18 at 13:43
  • Which line is the error on? – user202729 Oct 28 '18 at 13:43
  • 1
    i agree with John the exception is indicating that you are asking for a position in an array that doesn't exist. So it looks like either your values array or your data array is too small – JBoothUA Oct 28 '18 at 13:44
  • @JBooth Yeah, the 3 scenarios I see: 1) the line read contains 1 column (or is blank). 2) There aren't 5 rows. 3) The _guaranteed error_: OP is trying to reference columns 3 and 4 when they've only added 2 items to the row. 4) The last line would also cause issues for the same reason as 3. – ProgrammingLlama Oct 28 '18 at 13:47
  • 1
    `...when I think my code should work` Isnt that always the case, though – Ňɏssa Pøngjǣrdenlarp Oct 28 '18 at 13:48
  • Why are you splitting lines with semicolon instead of comma? Isn't .csv file comma delimited? – roozbeh S Oct 28 '18 at 13:53
  • 2
    @roozbehS: You can freely pick the field and row delimiters in CSV files. Usually to get around too many excape conflicts with the data (you need the comma because you run outside of the US where Comma is used as decimal seperator). So actualy Comma Seperated Value is a misnomer. – Christopher Oct 28 '18 at 14:04
  • @Christopher I know, but since we don't event know that which line of code throws the exception and this is a **code for school** and comma is the default separator when you save an excel file as .csv then I guessed that seems a mistake any beginner may make. – roozbeh S Oct 28 '18 at 14:39

1 Answers1

2

Initially, I'd change your last line from

File.WriteAllText(path, data[0][0] + delimiter + data[0][1] ....

to something like

var obj1 = data[0][0];
var obj2 = data[0][1];

File.WriteAllText(path, obj1 + delimiter + obj2 .... etc)

If you over inline functions or array accessing, when you get an exception the stack trace won't be that helpful. At least you'll have an idea of the statement that caused the issue.

This technique can prove to be very helpful, if you are looking at an in exception in the logs, after the fact.

Ralph Willgoss
  • 11,750
  • 4
  • 64
  • 67
  • There is not even any need to worry about performance. Outside of the debug builds, the JiT will cut out those temproary variables if deemed beneficial. It can even do the opposite and add temporary variables to optimize array access. – Christopher Oct 28 '18 at 14:02