2017-05-10 19 views
0

解決策を見つけようとしばらく私の頭を傷つけています。何も見つかりませんでしたGoogleで検索するときに関連するので、この問題の原因を突き止めようと数時間後に、私はここで助けを求めようとしています..VB.net - DataGridviewに行を追加すると、InvalidArgument = Value '8797'が 'rowIndex'に無効です。

奇妙なことは、通常、数千の行が検査された後のランダムな時間。

アプリケーション自体はリンク抽出プログラムで、異なるURIからリンクを抽出し、新しい内部リンクを見つけたら、それらをデータグリッドビューに追加します。

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

それは本当に私には意味がありませんSee image here

またはコードで ..:

私はIntelliTraceの例外のいくつかを見て、それはエラーがこの行でスローされると言いますなぜこのような高いインデックスの整数を投げるのでしょうか?特にこの行ではなく、新しい行を追加するだけです。とにかく、インデックスはたぶん1つのポイントに存在していましたが、スキャンごとに、 x文字列を含んでいません。そして、リンクがなくなるまで、リンクを再度スキャンします。私もライン「それぞれについて」で、またはとNullReferenceException「オブジェクト参照がオブジェクトのインスタンスに設定されていない」受信

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:\Users\USER\Documents\Visual Studio 2010\Projects\Link\Link\Form1.vb:linje 568 

:それは任意の助けになることができればここで

は、スタックトレースです。私は、セル(0)が何らかの理由でnullの場合、これが当てはまるかもしれないと思います - 私はこれを今チェックしますが、まだ最初のものは説明しません。これはスタックトレースである

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

:それはhtmlページを受信した後

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:\Users\USER\Documents\Visual Studio 2010\Projects\Link\Link\Form1.vb:linje 808 

EDIT はここで、いくつかのより多くのコードです。

If Not data = "" Then 

        Dim links As MatchCollection = Regex.Matches(data, "<a.*?href=[""']?([^'"">\ ]*)[""']?[^>]*>([\s\S]*?)<\/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 

答えて

0

HttpUtility.HtmlDecode(matchUrl)はあなたに無効なデータを与えます。あなたのコードスニペットはContinue Forなので、私は2 Forのループを持っていると仮定しています。

  1. matchUrlには有効な値がありますか?
  2. 行に追加する前とIf条件で使用する前に、HttpUtility.HtmlDecode(matchUrl)の値を検証します。 anchor_txtを検証しますが。
  3. コードスニペットには、TryブロックとCatchブロックが表示されません。あなたがそれを使用していない場合は、そうしてください。
  4. exists = Trueの文の後にForのループ内にExit Forを使用します。
  5. コードは、2つの異なるインスタンスでHttpUtility.HtmlDecode(matchUrl)を評価し、内部ループで複数回評価されます。その値を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 
+0

を私はmatchurlが正の値を返すことを肯定しています。そして、はい、私は各ループ2、メインの1つは、それがHTMLページのすべてのリンクをチェックし、2番目のそれは、DataGridviewで重複をチェックします。私はmatchurlがコードの上に何もないかどうか実際に確認します。 Try catchをもう一度追加しました.HTTPutility htmldecodeを複数回呼び出すので、定義済みの重複文字列が見つかったときにExitを追加しました。 – JDoe

+0

私は変更を加えてアプリケーションを再実行しましたが、それでも2つのエラーが返されました。上記のコードを追加して、コードの理解を深めることができます。 – JDoe

+0

@JDoe、あなたは 'urlDecode = HttpUtility.HtmlDecode(matchUrl)'にブレークポイントを入れてみましたか? urlDecodeの値は何ですか?それは正しいのでしょうか? –

関連する問題