-
Apr 12th, 2013, 02:13 PM
#1
Thread Starter
New Member
[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
Last edited by dougdmusa; Apr 12th, 2013 at 02:51 PM.
-
Apr 12th, 2013, 02:16 PM
#2
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.
-
Apr 12th, 2013, 02:50 PM
#3
Thread Starter
New Member
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
-
Apr 12th, 2013, 02:54 PM
#4
Re: Object Reference not set to an instance of an object
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().
-
Apr 12th, 2013, 03:19 PM
#5
Re: [RESOLVED] Object Reference not set to an instance of an object
Nope, it is THE worst possible way to go about it.
My usual boring signature: Nothing
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|