Jump to content
Xtreme .Net Talk

WHY?: An unhandled exception of type 'System.IndexOutOfRangeException' occurred..


Recommended Posts

Posted

I am trying to get the google API to give me more than 10 results back from a search.

 

I keep getting this error:

 

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in VB Google Web APIs.exe

 

Additional information: Index was outside the bounds of the array.

 

and it highlights:

TextBox1.Text = TextBox1.Text & vbNewLine & objResults.resultElements(shtCount).URL

 

Here is my code:

 

'Option Strict On

Public Class Form1
   Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

   Public Sub New()
       MyBase.New()

       'This call is required by the Windows Form Designer.
       InitializeComponent()

       'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.IContainer

   'NOTE: The following procedure is required by the Windows Form Designer
   'It can be modified using the Windows Form Designer.  
   'Do not modify it using the code editor.
   Friend WithEvents label2 As System.Windows.Forms.Label
   Friend WithEvents lblLicense As System.Windows.Forms.Label
   Friend WithEvents txtLicenseKey As System.Windows.Forms.TextBox
   Friend WithEvents lblSearchResults As System.Windows.Forms.Label
   Friend WithEvents txtSearchTerm As System.Windows.Forms.TextBox
   Friend WithEvents btnSearch As System.Windows.Forms.Button
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.lblSearchResults = New System.Windows.Forms.Label
       Me.label2 = New System.Windows.Forms.Label
       Me.txtSearchTerm = New System.Windows.Forms.TextBox
       Me.lblLicense = New System.Windows.Forms.Label
       Me.txtLicenseKey = New System.Windows.Forms.TextBox
       Me.btnSearch = New System.Windows.Forms.Button
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       '
       'lblSearchResults
       '
       Me.lblSearchResults.BackColor = System.Drawing.SystemColors.Window
       Me.lblSearchResults.ForeColor = System.Drawing.SystemColors.WindowText
       Me.lblSearchResults.Location = New System.Drawing.Point(120, 96)
       Me.lblSearchResults.Name = "lblSearchResults"
       Me.lblSearchResults.Size = New System.Drawing.Size(240, 16)
       Me.lblSearchResults.TabIndex = 20
       '
       'label2
       '
       Me.label2.Location = New System.Drawing.Point(8, 96)
       Me.label2.Name = "label2"
       Me.label2.Size = New System.Drawing.Size(112, 16)
       Me.label2.TabIndex = 19
       Me.label2.Text = "Est. # Results:"
       Me.label2.TextAlign = System.Drawing.ContentAlignment.TopRight
       '
       'txtSearchTerm
       '
       Me.txtSearchTerm.Location = New System.Drawing.Point(8, 64)
       Me.txtSearchTerm.Name = "txtSearchTerm"
       Me.txtSearchTerm.Size = New System.Drawing.Size(280, 20)
       Me.txtSearchTerm.TabIndex = 18
       Me.txtSearchTerm.Text = "Enter search term"
       '
       'lblLicense
       '
       Me.lblLicense.Location = New System.Drawing.Point(8, 16)
       Me.lblLicense.Name = "lblLicense"
       Me.lblLicense.Size = New System.Drawing.Size(104, 16)
       Me.lblLicense.TabIndex = 15
       Me.lblLicense.Text = "License Key:"
       '
       'txtLicenseKey
       '
       Me.txtLicenseKey.Location = New System.Drawing.Point(120, 16)
       Me.txtLicenseKey.Name = "txtLicenseKey"
       Me.txtLicenseKey.ReadOnly = True
       Me.txtLicenseKey.Size = New System.Drawing.Size(240, 20)
       Me.txtLicenseKey.TabIndex = 29
       Me.txtLicenseKey.Text = "9infUG9QFHK3giE52CIiAC/Sups99Hh4"
       '
       'btnSearch
       '
       Me.btnSearch.Location = New System.Drawing.Point(296, 64)
       Me.btnSearch.Name = "btnSearch"
       Me.btnSearch.Size = New System.Drawing.Size(64, 24)
       Me.btnSearch.TabIndex = 30
       Me.btnSearch.Text = "Search"
       '
       'TextBox1
       '
       Me.TextBox1.Location = New System.Drawing.Point(40, 144)
       Me.TextBox1.MaxLength = 32767600
       Me.TextBox1.Multiline = True
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
       Me.TextBox1.Size = New System.Drawing.Size(312, 136)
       Me.TextBox1.TabIndex = 31
       Me.TextBox1.Text = ""
       '
       'Form1
       '
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(376, 293)
       Me.Controls.Add(Me.TextBox1)
       Me.Controls.Add(Me.btnSearch)
       Me.Controls.Add(Me.txtLicenseKey)
       Me.Controls.Add(Me.lblSearchResults)
       Me.Controls.Add(Me.label2)
       Me.Controls.Add(Me.txtSearchTerm)
       Me.Controls.Add(Me.lblLicense)
       Me.Name = "Form1"
       Me.Text = "Google Web APIs Demo (VB)"
       Me.ResumeLayout(False)

   End Sub

#End Region

   ' Search button: do a search, display number of results 
   Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

       Dim X As Int64

       X = 0
top:
       ' Create a new Google search object
       Dim objSearch As New Google.GoogleSearchService
       ' Invoke the search method
       Dim objResults As Google.GoogleSearchResult = objSearch.doGoogleSearch(txtLicenseKey.Text, txtSearchTerm.Text, X, 10, False, "", False, "", "", "")

       Dim estResults As Int64 = objResults.estimatedTotalResultsCount
       lblSearchResults.Text = CStr(estResults)

       ' Loop through result elements
       Dim shtCount As Short
       For shtCount = 0 To objResults.endIndex - 1
           'MessageBox.Show(objResults.resultElements(shtCount).title & " - " & objResults.resultElements(shtCount).URL)
           TextBox1.Text = TextBox1.Text & vbNewLine & objResults.resultElements(shtCount).URL
       Next

       X = X + 10
       If X < 30 Then GoTo top

   End Sub

End Class

 

 

Here is a screenshot of my window:

http://webpages.charter.net/trend/error.JPG

 

I am not sure if this is a limitation on google's API.. or justa coding error. Actually.. now I think about it.. This if loop only runs only 2 or 3 times.. OR if I make X = 100, the program errors out with only one go at the if loop.

 

 

any ideas?

 

thanks!

Lee

Posted
Are you sure there even are any resultElements in objResults? Perhaps it is the first (0) element that is out of range. You don't seem to be checking for that possibility.
Posted
Are you sure there even are any resultElements in objResults? Perhaps it is the first (0) element that is out of range. You don't seem to be checking for that possibility.

 

Interesting...

 

I will check into that.

 

 

thanks!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...