-1

I have a problem with my code. During debug mode, there is no error or warning at all. But when i pressed a button, an error comes up. This is my code:

Private Sub Button11_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    Button1.Enabled = True
    Dim i As Integer
    For i = 0 To 10


        WebBrowser1.Document.GetElementById("login").SetAttribute("value", (TextBox1.Text))
        WebBrowser1.Document.GetElementById("saveBtn").InvokeMember("click")

   Next i
End Sub
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Jun 19 '14 at 15:28
  • WebBrowser1.Document.GetElementById("login") will return "NOTHING" if the element is not found. You should inspect this first to make sure it is NOT NOTHING before calling other methods on it. – Steve Jun 19 '14 at 16:56
  • RE: debug - check your Debug, Options and Settings... Uncheck My Code and Break when exceptions cross domains... to see more errors in debug. – rheitzman Jun 19 '14 at 19:29

1 Answers1

0

You should, as Steve said in the comments, use some kind of Error handling to your code...i.e try....catch method to make sure that everything is valid in the WebBrowser. Try something like this

Private Sub Button11_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    Button1.Enabled = True
    Dim i As Integer
    For i = 0 To 10
        Try
            WebBrowser1.Document.GetElementById("login").SetAttribute("value", (TextBox1.Text))
            WebBrowser1.Document.GetElementById("saveBtn").InvokeMember("click")
        Catch ex As Exception
        End Try
    Next i
End Sub

Try...Catch blocks are just methods to detect problems. In your case, lets say only few of the command inside the loop returned NULL...By using this method, you can "ignore" these problems and move on to the next "i" without throwing an exeption or dealing with wrong texts etc.

ILoveMom
  • 66
  • 7