Dim webbrowser2 As New WebBrowser
webbrowser2.Navigate("http://facebook.com")
Threading.Thread.Sleep(5000)

If facebooklogin = False Then
If webbrowser2.Document.All Is Nothing Then
webbrowser2.Navigate("http://facebook.com")
Threading.Thread.Sleep(5000)
End If

Dim allelements As HtmlElementCollection = webbrowser2.Document.All 'Returns Object Reference not set to the instance of an object


webbrowser2.Document.GetElementById("email").SetAttribute("value", (txt_facebookEmail.Text))
webbrowser2.Document.GetElementById("pass").SetAttribute("value", (txt_facebookPassword.Text))
For Each webpageelement As HtmlElement In allelements

If webpageelement.GetAttribute("tabindex") = "4" Then

webpageelement.InvokeMember("click")

End If
Next
facebooklogin = True
Else

End If

Hey guys, I am trying to click a button on a website with a WebBrowser control. This worked fine before, but once I put it into it's own thread, it returned an Object Reference not set to the instance of an object.

Any idea why it has done this? Any solutions?
The line that returns the error is indicated in the code.

Thanks guys!