0

Sometimes, my application throws random index out of range exceptions, and I'm a bit lost to what the reason is..

Here's a screenshot: enter image description here

As you can see the index (thi3) is under the rows count, so I don't see the problem??

Edit: This is where I declare the columns for the datatable (happens on form load)

InternalDataTable.Columns.Add("Domain")
InternalDataTable.Columns.Add("Anchor text")
InternalDataTable.Columns.Add("Status")
InternalDataTable.Columns.Add("E. Links")
InternalDataTable.Columns.Add("Thread #")
InternalDataTable.Columns.Add("Tjek", GetType(Boolean))

DataGridView1.DataSource = InternalDataTable

Edit2: I now received an error again, but now at the line below the highlighted line in the image, so indeed the problem is something with the item(x)

If InternalDataTable.Rows(thi3).Item(2) = "" Then

Edit3: Once again, the error occured, see screenshot here https://i.gyazo.com/a7cc582e4cbf33bea59a8efb9bb36497.png - (cant embed images yet) I know i should post code, but this image gives a view that both columns and rows & items are within index.

JDoe
  • 485
  • 1
  • 5
  • 14
  • 1
    Change your [And with AndAlso](http://stackoverflow.com/questions/302047/what-is-the-difference-between-and-and-andalso-in-vb-net), Next time do not post images of code. Post directly the code – Steve May 19 '17 at 20:54
  • Roger that. Thanks, I will give that a shot. – JDoe May 19 '17 at 20:55
  • As @Steve says, you should use `AndAlso` to prevent the second expression being evaluated when `thi3` is too large. However, from your screenshot, that doesn't seem to be the problem here. In that case, the problem could be that `InternalDataTable.Rows(thi3)` has less than one `Item`. – Blackwood May 19 '17 at 23:44
  • @Blackwood - Yes, but see the latest screenshot though. Both rows and columns are within the index. – JDoe May 20 '17 at 11:49

1 Answers1

0

The problem in your code is that you may have inserted several rows, but you don't have any Columns

InternalDataTable.Rows(thi3).Item(0) = ""

The .Item(0) can be causing theindex out of range exception.

Also, I recommend you changing the whole if to this one:

if thi3 < InternalDataTable.Rows.Count andalso InternalDataTable.Columns.Count > 2  andalso InternalDataTable.rows(Thi3).Item(0) <> "" then

Attention to change the <= to < since accessing the Rowcount index will also generate the index out of range exception.

Quima
  • 894
  • 11
  • 21
  • I do have the columns added, posted code of where I add them. But yeah, that have crossed my mind as well. I'm running my application again, waiting to see if it will throw any exceptions this time.. – JDoe May 20 '17 at 09:56