2

Alright so I am new here so I apologize in advance if I post incorrectly or am a little vague. My problem is that I run into a NullReferenceException when I try to run my code but while debugging and hovering my mouse over the problematic variable, I do indeed see the value of the variable.

Here is the VB code that I am working with:

Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles login.Click
    status.Text = "Connecting...."

    WebBrowser2.Navigate("http://*****.com/?op=login")
    WebBrowser2.Document.GetElementById("loginUsername").InnerText = username.Text
    WebBrowser2.Document.GetElementById("loginPassword").InnerText = password.Text
    WebBrowser2.Document.GetElementById("loginSubmit").InvokeMember("click")

End Sub

Here is the snapshot of what is going on:

screenshot

------------ EDIT : SOLUTION -------------------

    WebBrowser2.Url = New Uri("http://*****.com/?op=login")
    WaitForPageLoad() ' <---------- ADDED NEW FUNCTION TO WAIT FOR PAGE LOAD

    WebBrowser2.Document.GetElementById("loginUsername").InnerText = username.Text
    WebBrowser2.Document.GetElementById("loginPassword").InnerText = password.Text
    WebBrowser2.Document.GetElementById("loginSubmit").InvokeMember("click")

    status.Text = "Completed"

So I created a new function (credits go to BGM in How to wait until WebBrowser is completely loaded in VB.NET?) called WaitForPageLoad() which essentially loops through a check for the page to be ready and then once it is, is kills the handler so the login is successful and the page does not loop. Here is the WaitForPageLoad():

Private Property pageready As Boolean = False

Private Sub WaitForPageLoad()
    AddHandler WebBrowser2.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser2.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser2.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub
Community
  • 1
  • 1
Omar J.
  • 95
  • 2
  • 10

3 Answers3

5

WebBrowser2.Navigate takes some time to load the document, but is asynchronous. That means that the next code gets executed before the document finishes loading.

Consequently, in the next line, GetElementById cannot yet find the target element and returns Nothing. To prevent this, you cannot execute code after calling Navigate – instead, you need to create an event handler for the event that is fired once the document finished loading, and execute the code there. – This is the DocumentCompleted event.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • That was exactly the problem! Once created a function to wait for the page to load, the null elements were found and the code executed nicely. Thank you. – Omar J. Jul 26 '12 at 17:12
1

On that line in particular...

  • Document could be null
  • The result of GetElementById("loginUsername") could be null.
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
0

Why do you think that username is null?

I bet that WebBrowser2.Document.GetElementById("loginUsername") returns null.

The other possibility is Document to be null.

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119