title: Message Box preventing exception, but requires user input
body: <p>This code gets images from google, and sets a Picturebox's image to the specified index. For some reason it will not work without, &quot;MsgBox(&quot;anytext&quot;)&quot; being right before &quot;Dim hecImages as HtmlElementCollection...&quot;</p>
  <pre><code>Private Sub GetImagesFromUrl()
         Dim wbID As New WebBrowser
  
         wbID.Navigate(&quot;https://www.google.com/search?q=&quot; + TextBox2.Text + &quot;+ background&quot; + &quot;&amp;tbm=isch&quot;)
         Dim wcObj As New System.Net.WebClient() 'Create New Web Client Object
         MsgBox(&quot;anytext&quot;)
         Dim hecImages As HtmlElementCollection = wbID.Document.GetElementsByTagName(&quot;img&quot;) 'Browse Through HTML Tags
  
         Dim strWebTitle As String 'Web Page Title
         Dim strPath1 As String 'Folder Path
         Dim strPath2 As String 'Sub Folder Path
  
         strWebTitle = wbID.DocumentTitle  'Obtain Web Page Title
  
         strPath1 = Application.StartupPath + &quot;\content\images\&quot; &amp; strWebTitle 'Create Folder Named Web Page Title
         strPath2 = strPath1 &amp; &quot;\Images&quot; 'Create Images Sub Folder
  
         Dim diTitle As DirectoryInfo = Directory.CreateDirectory(strPath1)
         Dim diImages As DirectoryInfo = Directory.CreateDirectory(strPath2)
  
         For i As Integer = 0 To hecImages.Count - 1 'Loop Through All IMG Elements Found
  
             'Download Image(s) To Specified Path
  
             wcObj.DownloadFile(hecImages(i).GetAttribute(&quot;src&quot;), strPath1 &amp; &quot;\images\&quot; &amp; i.ToString() &amp; &quot;.jpg&quot;)
         Next
         '  MsgBox(strPath2)
         PictureBox1.Image = Image.FromFile(strPath2 + &quot;\1.jpg&quot;)
     End Sub
  </code></pre>
  <p>Removing the messagebox only causes &quot;hecImages&quot; to return nothing.</p>
  <pre><code>Exception Thrown:
  
  System.NullReferenceException: 'Object reference not set to an instance of an object.'
  
  System.Windows.Forms.WebBrowser.Document.get returned Nothing.
  
  </code></pre>
  <p>I want it to be able to do all this without the user pressing anything</p>
  <hr />
  <pre><code>'Fixed Version
  
  #Region &quot;Get Images from URL&quot;
      Private WithEvents wbID As New WebBrowser
      Private Sub GetImagesFromUrl()
          Dim wbCleanPath = Path.Combine(&quot;https://www.google.com/search?q=&quot; + Form2.TextBox2.Text + &quot;+ background + banner&quot; + &quot;&amp;tbm=isch&quot;)
          wbID.Navigate(wbCleanPath)
      End Sub
      Private Sub wbID_DlFinished(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles wbID.DocumentCompleted
          If wbID.ReadyState = WebBrowserReadyState.Complete Then
              Dim wcObj As New System.Net.WebClient() 'Create New Web Client Object
              'MsgBox(&quot;anytext&quot;)
              Dim hecImages As HtmlElementCollection = wbID.Document.GetElementsByTagName(&quot;img&quot;) 'Browse Through HTML Tags
              Dim strWebTitle As String 'Web Page Title
              Dim strPath1 As String 'Folder Path
              Dim strPath2 As String 'Sub Folder Path
              strWebTitle = wbID.DocumentTitle  'Obtain Web Page Title
              strPath1 = Path.Combine(Application.StartupPath + &quot;\content\images\&quot; &amp; strWebTitle) 'Create Folder Named Web Page Title
              strPath2 = Path.Combine(strPath1 &amp; &quot;\Images&quot;) 'Create Images Sub Folder
              Dim diTitle As DirectoryInfo = Directory.CreateDirectory(strPath1)
              Dim diImages As DirectoryInfo = Directory.CreateDirectory(strPath2)
              For i As Integer = 0 To 1 'Loop Through All IMG Elements Found
                  'Download Image(s) To Specified Path
                  wcObj.DownloadFile(hecImages(i).GetAttribute(&quot;src&quot;), Path.Combine(strPath1 &amp; &quot;\images\&quot; &amp; i.ToString() &amp; &quot;.jpg&quot;))
              Next
              '  MsgBox(strPath2)
              PictureBox1.Image = Image.FromFile(Path.Combine(strPath2 + &quot;\1.jpg&quot;))
              File.Delete(Path.Combine(strPath2 + &quot;\0.jpg&quot;))
              wbID.Dispose()
              wcObj.Dispose()
          Else
              'donothing
          End If
      End Sub
  #End Region
  </code></pre>