-1

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);
            }
        }
    }
Isaac
  • 51
  • 1
  • 9
  • 2
    And the stack trace of the error? – Mohammad Dehghan Oct 29 '20 at 19:54
  • 1
    Are you sure the error is in the writeline? Looking at your code, I think you're using the rowCount variable instead of the i... You do a loop, but inside the loop you use the rowCount variable instead of the i. And, if NUMBER_OF_ENTRIES is the size of the URLs array, the line before the Console.WriteLine will fail for IndexOutOfRangeException, because if the size of the array is 100, the last element is 99 – PiGi78 Oct 29 '20 at 20:41
  • 1
    Please review [MCVE] guidance on posting code and [edit] the question. It is very unlikely `Console.WriteLine(", '{0}'", index_val);` can produce that error (`Console.WriteLine(", '{1}'", index_val);` would fail with exception that is similar but of different type - `FormatException`) – Alexei Levenkov Oct 29 '20 at 20:48

1 Answers1

2

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;
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61