Results 1 to 5 of 5

Thread: [RESOLVED] Object Reference not set to an instance of an object

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Location
    Alexandria VA
    Posts
    10

    Resolved [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.

  2. #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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Location
    Alexandria VA
    Posts
    10

    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

  4. #4

    Re: Object Reference not set to an instance of an object

    Quote Originally Posted by dougdmusa View Post
    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().

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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
  •  



Click Here to Expand Forum to Full Width