Search code examples
c#excelstringindexoutofrangeexceptionconsole.writeline

Why do I keep getting a System.IndexOutOfRangeException at my Console.WriteLine(", '{0}'", String); statement?


I'm working on a web crawler in C# in Visual Studio that reads URLs from an Excel spreadsheet into an array of strings that will be gone through later to visit sites to scrape information from. I am debugging it right now, but for some reason I get an out of range exception whenever I try to print the string to the console.

I'm using the IronXL Excel interfacing library to read and write from/to the Excel spreadsheet. And the exception keeps happening on the line where the "Console.WriteLine(", '{0}'", String);" statement is located.

Here are the class variables involved:

    public static int NUMBER_OF_ENTRIES = 90;
    public static String [] URLs = new String [NUMBER_OF_ENTRIES];
    public static String PATH_OF_IO_DOC = "C:\\Users\\Owner\\Desktop\\List of Clubs.xlsx";
    public static String SHEET_NAME = "Sheet1";

And here is the code of the method involved:

private void buttonGetURLs_Click(object sender, EventArgs e)
    {
        //read the URLs from the excel doc to an array of strings
        WorkBook wb = WorkBook.Load(PATH_OF_IO_DOC);
        WorkSheet ws = wb.GetWorkSheet(SHEET_NAME);

        int rowCount = NUMBER_OF_ENTRIES;
        //start at row 2 to skip the first header
        for (int i = 2; i < rowCount; i++)
        {
            //skip header lines
            if (!((rowCount == 13) || (rowCount == 16) || (rowCount == 31) || (rowCount == 32) || (rowCount == 33)))
            {
                //get value by cell address
                //string address_val = ws["A" + rowCount].ToString();
                //get value by row and column indexing
                string index_val = ws.Rows[rowCount].Columns[1].ToString();

                //read each cell's value to the array of URLs
                URLs[rowCount] = index_val;
                
                //check to make sure correct values are collected
                Console.WriteLine(", '{0}'", index_val);
            }
        }
    }

Solution

  • That's the faulty line as URLs will reach up to rowCount-1 being 0 based:

    URLs[rowCount] = index_val;
    

    You might want to use the i index

    URLs[i] = index_val;