VB.net - 向数据网格视图添加行将返回无效参数=值'8797'对'rowIndex'无效



我一直在挠头试图找到解决方案,在谷歌上搜索时我找不到任何相关的东西,所以经过几个小时试图找到这个问题背后的原因,我试图在这里寻求帮助。

奇怪的是,错误不会立即发生,而是在检查了几千行之后随机发生。

应用程序本身是一个链接提取器,它从不同的 URI 中提取链接,然后在找到新的内部链接时,将它们添加到 datagridview 中。

当我查看一些 IntelliTrace 异常时,它说错误在以下行引发: 看图片

这里或者在代码中..

DataGridView1.Rows.Add(New String() {HttpUtility.HtmlDecode(matchUrl), anchor_txt, "", "", "", True})

对我来说,为什么它会抛出如此高的索引整数并没有真正的意义,尤其是在这一行,当它所做的只是添加一个新行时。无论如何,索引可能曾经存在过,但是对于每次扫描,我都会删除不包含 x 字符串的 URI,然后再次扫描链接,直到没有链接。

这是一个堆栈跟踪,如果它可以有任何帮助:

System.ArgumentException: InvalidArgument=Værdi '8797' er ugyldig for 'rowIndex'.
ved System.Windows.Forms.DataGridViewRow.GetState(Int32 rowIndex)
ved System.Windows.Forms.DataGridViewRowCollection.GetRowState(Int32 rowIndex)
ved System.Windows.Forms.DataGridViewRowCollection.UpdateRowCaches(Int32 rowIndex, DataGridViewRow& dataGridViewRow, Boolean adding)
ved System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PreNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow& dataGridViewRow, Boolean changeIsInsertion)
ved System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount)
ved System.Windows.Forms.DataGridViewRowCollection.AddInternal(Boolean newRow, Object[] values)
ved System.Windows.Forms.DataGridViewRowCollection.Add(Object[] values)
ved Link_Extractor.Form1.InternalThread2() i C:UsersUSERDocumentsVisual Studio 2010ProjectsLinkLinkForm1.vb:linje 568

我还在"对于每个"行收到"对象引用未设置为对象的实例"或 NullReferenceException。我想这可能是如果 cell(0) 由于某种原因为 null - 我现在将检查这一点,但仍然没有解释第一个。

For Each itm As DataGridViewRow In DataGridView1.Rows
If itm.Cells(0).Value = HttpUtility.HtmlDecode(matchUrl) Then
exists = True
End If
Next

其中这是堆栈跟踪:

System.NullReferenceException: Objektreferencen er ikke indstillet til en forekomst af et objekt.
ved System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount)
ved System.Windows.Forms.DataGridViewRowCollection.AddInternal(Boolean newRow, Object[] values)
ved System.Windows.Forms.DataGridViewRowCollection.Add(Object[] values)
ved Link_Extractor.Form1.InternalThread3() i C:UsersUSERDocumentsVisual Studio 2010ProjectsLinkLinkForm1.vb:linje 808

编辑这是在它收到 html 页面后的其他代码。

If Not data = "" Then
Dim links As MatchCollection = Regex.Matches(data, "<a.*?href=[""']?([^'""> ]*)[""']?[^>]*>([sS]*?)</a>")
For Each match As Match In links
Dim matchUrl As String = HttpUtility.HtmlDecode(match.Groups(1).Value)
Dim anchor As String = HttpUtility.HtmlDecode(StripTags(match.Groups(2).Value))
'Ignore all anchor links
If matchUrl.StartsWith("#") Then
Continue For
End If
'Ignore all javascript calls
If matchUrl.ToLower.StartsWith("javascript:") Then
Continue For
End If
'Ignore all email links
If matchUrl.ToLower.StartsWith("mailto:") Then
Continue For
End If
'Ignore all URLs with @
If matchUrl.ToLower.Contains("@") Then
Continue For
End If
'Ignore all empty domains
If matchUrl Is Nothing Then
Throw New Exception("Empty matchurl")
End If
If anchor Is Nothing Then
Throw New Exception("Empty anchor text.")
End If
'For internal links, build the url mapped to the base address
If Not matchUrl.StartsWith("http://") And Not matchUrl.StartsWith("https://") Then
'Højst sansynligt internt link
matchUrl = MapUrl(url, matchUrl)
Try
exists = False
For Each itm As DataGridViewRow In DataGridView1.Rows
If Not itm.Cells(0) Is Nothing Then
If itm.Cells(0).Value = matchUrl Then
exists = True
Exit For
End If
End If
Next
If DataGridView1.Rows.Count > 0 AndAlso exists = True Then
Continue For
Else
DataGridView1.Rows.Add(New String() {matchUrl, anchor, "", "", "", True})
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Else
'It's possible that it still can be an internal link, but also external. Compare the baseaddress with the URL to check if it's still the same domain
Dim baseaddress As Uri = New Uri(url)
Dim s_baseaddress As String = baseaddress.Host.ToString
'Check for subdomain and remove
If s_baseaddress.ToCharArray().Count(Function(c) c = "."c) >= 2 Then
Dim subdomain As String = Split(s_baseaddress, ".").First
s_baseaddress = s_baseaddress.Replace(subdomain & ".", "")
End If
Dim s_url As String = Nothing
Dim url2 As Uri = Nothing
Try
url2 = New Uri(HttpUtility.HtmlDecode(matchUrl))
s_url = url2.Host.ToString
If s_url.ToCharArray().Count(Function(c) c = "."c) >= 2 Then
Dim subdomain As String = Split(s_url, ".").First
s_url = s_url.Replace(subdomain & ".", "")
End If
Catch ex As Exception
'Invalid URI
Continue For
End Try
If s_baseaddress.Equals(s_url) Then
'Internal
Try
exists = False
For Each itm As DataGridViewRow In DataGridView1.Rows
If Not itm.Cells(0) Is Nothing Then
If itm.Cells(0).Value = matchUrl Then
exists = True
Exit For
End If
End If
Next
If DataGridView1.Rows.Count > 0 AndAlso exists = True Then
Continue For
Else
DataGridView1.Rows.Add(New String() {matchUrl, anchor, "", "", "", True})
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Else
'External link
Dim m_url As String = matchUrl
m_url = m_url.Replace(" ", "")
'Trim url to root to save the time of removing duplicates
Dim theUri = Nothing
Try
theUri = New Uri(m_url)
Catch ex As Exception
'Invalid link, go to next
Continue For
End Try
Dim theDomain = theUri.GetLeftPart(UriPartial.Authority)
Try
exists = False
For Each itm As DataGridViewRow In DataGridView2.Rows
If Not itm.Cells(0) Is Nothing Then
If itm.Cells(0).Value = theDomain.ToString Then
exists = True
Exit For
End If
End If
Next
If DataGridView2.Rows.Count > 0 AndAlso exists = True Then
Continue For
Else
DataGridView2.Rows.Add(New String() {theDomain.ToString, anchor, ""})
e_links_c += 1
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End If
Next
'OK
DataGridView1.Rows(thi3).Cells(2).Value = "OK"
DataGridView1.Rows(thi3).Cells(3).Value = e_links_c.ToString
Else
'Error
DataGridView1.Rows(thi3).Cells(2).Value = "Empty response"
End If

似乎HttpUtility.HtmlDecode(matchUrl)给你无效的数据。由于您的代码片段具有Continue For,我的假设是您有 2 个For循环。

  1. 您确定matchUrl具有有效值吗?
  2. 添加到行之前以及在 If 条件中使用它之前,请验证HttpUtility.HtmlDecode(matchUrl)值。虽然你验证了anchor_txt.
  3. 代码片段不显示TryCatch块。如果您不使用它,请这样做。
  4. 在语句之后的内部For循环中使用Exit Forexists = True
  5. 代码在两个不同的实例上计算HttpUtility.HtmlDecode(matchUrl),并在内部For循环中多次计算。在 For 循环之前将其值分配给变量。验证输出,然后在循环中使用此值For

尝试以下代码:-

Try
exists = False
urlDecode = HttpUtility.HtmlDecode(matchUrl)  'Put a break point on this statement and check the value of matchUrl and HttpUtility.HtmlDecode(matchUrl)
If urlDecode is Nothing Then  
Throw New Exception("Empty urlDecode.")
End If
For Each itm As DataGridViewRow In DataGridView1.Rows
If itm.Cells(0).Value = urlDecode Then
exists = True
Exit For
End If
Next
If DataGridView1.Rows.Count > 0 AndAlso exists then
Continue For
Else
Dim anchor_txt As String = HttpUtility.HtmlDecode(StrpTags(match.Groups(2).Value))
If urlDecode is Nothing Then
Throw New Exception("Empty anchor text.")
End If
DataGridView1.Rows.Add(New String() {HttpUtility.HtmlDecode(matchUrl), anchor_txt, "", "", "", True})
End If      
Catch ex As Exception
' Show the exception's message.
MessageBox.Show(ex.Message)
End Try

最新更新