1

I'm working on some code in VB.NET which will do some things automatically. I have a couple of buttons which will do some actions manually. But I also have one button that does some actions automatically.

If I press the buttons to run it manually, it works fine. But when I press the button to run it automatically, it gives me an Object reference not set to an instance of an object exception.

This is the code that gives the exception:

Private Sub btnStep4_Click(sender As Object, e As EventArgs) Handles btnStep4.Click

Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

Dim ran As Boolean = False
For Each webpageelement As HtmlElement In allelements
    WebBrowser1.Document.GetElementById("website_name").SetAttribute("value", txtWebName.Text + txtWebExt.Text) '< This one
    WebBrowser1.Document.GetElementById("website_code").SetAttribute("value", txtWebName.Text) '< And this one


    If webpageelement.GetAttribute("title") = "Website opslaan" And ran = False Then

        webpageelement.InvokeMember("click")
        ran = True
    End If
Next


End Sub

So when I click the btnStep4 button, it works. But when I simulate the click like this:

While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        End While
        btnStep4_Click(sender, e)

I get the exception.

Why is this? Is there something I'm missing?

This might be a stupid mistake, but I just can't find out what I'm doing wrong.
Any help or tips would be greatly appreciated!

Sj03rs
  • 937
  • 10
  • 32
  • 1
    Pls avoid using [Application.DoEvents](http://stackoverflow.com/questions/5181777/use-of-application-doevents). What happens if you put your code into a normal method and call (simulate) it ? And also try it without DoEvents! – C0d1ngJammer Dec 28 '15 at 11:26
  • @C0dingJammer, the same thing happens. So what I understand is, I should use Threads? – Sj03rs Dec 28 '15 at 11:41
  • So the WebBrowser1 or the WebBrowser1.Document must be somehow null there. Are you shure that no other event is connected/linked with the button? – C0d1ngJammer Dec 28 '15 at 12:39

0 Answers0