[RESOLVED] Object Reference not set to an instance of an object
I am getting the error message as the title shows when I try to insert user input into a web page form, please disregard the comment under line 8.
NOTE: The error will be in line 7 "Dim job As HtmlElement = Me.WebBrowser1.Document.All.Item("JobName")"
Code:
Public Class Form1
Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
WebBrowser1.Navigate("http:///TEST/")
With WebBrowser1
Dim job As HtmlElement = Me.WebBrowser1.Document.All.Item("JobName")
job.InnerText = TextBox1.Text
' .Document.GetElementById("Job").SetAttribute("Value", TextBox1.Text)
End With
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
End Sub
End Class
Re: Object Reference not set to an instance of an object
The element you're looking for ("JobName") doesn't exist on the page because you're not waiting for the page to finish loading. The Navigate() method does not block until the page is done loading. What you want to do is handle the DocumentCompleted event of the WebBrowser and do your attribute retrieval and setting of text that way.
EDIT: Re-read the initial comment again, and I stand by what I said, just not the reason for the Exception. You still need to handle everything in the DocumentCompleted Event that I linked above.
Re: Object Reference not set to an instance of an object
I was able to correct this problem by using the ReadyState.Complete;
Code:
With Webbrowser
.Navigate("")
DoWhile .ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Re: Object Reference not set to an instance of an object
Quote:
Originally Posted by
dougdmusa
I was able to correct this problem by using the ReadyState.Complete;
Code:
With Webbrowser
.Navigate("")
DoWhile .ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
That is, quite possibly, one of the worst ways to go about it. The proper way is to handle the DocumentCompleted Event which requires almost no additional code on your part, and completely removes the necessity for a loop AND Application.DoEvents().
Re: [RESOLVED] Object Reference not set to an instance of an object
Nope, it is THE worst possible way to go about it.