-1

I'm trying to build my first HTML UI with Webbrowser component in VB.Net. I have found this code example on Microsoft site

https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.document(v=vs.110).aspx :

 Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
    Handles Me.Load

        WebBrowser1.DocumentText =
        "<html><body>Please enter your name:<br/>" &
        "<input type='text' name='userName'/><br/>" &
        "<a href='http://www.microsoft.com'>continue</a>" &
        "</body></html>"

    End Sub

    Private Sub webBrowser1_Navigating(
    ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs) _
    Handles WebBrowser1.Navigating

        Dim document As System.Windows.Forms.HtmlDocument =
        WebBrowser1.Document
        If document IsNot Nothing And
        document.All("userName") IsNot Nothing And
        String.IsNullOrEmpty(
        document.All("userName").GetAttribute("value")) Then

            e.Cancel = True
            MsgBox("You must enter your name before you can navigate to " &
            e.Url.ToString())
        End If

    End Sub

When I put it on to the test, most of the time throws exception 'System.NullReferenceException' in this part of the code:

If document IsNot Nothing And
        document.All("userName") IsNot Nothing And
        String.IsNullOrEmpty(
        document.All("userName").GetAttribute("value")) Then

Sometimes it works, but mostly it doesn't work at all. Any idea how to fix this? I'm very new to .Net platform and sorry if there is any miss spelling. Any help is appreciated.

admnelson
  • 1
  • 1
  • Change your `And` to `AndAlso`... Its a short circuit... – Trevor Jul 17 '16 at 05:43
  • 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) – Trevor Jul 17 '16 at 05:56

1 Answers1

-1

If document is nothing then the other clauses of the If statement will generate exceptions because you are attempting to access properties whilst document is Nothing. You need to rewrite the code like this:

Dim document As System.Windows.Forms.HtmlDocument = WebBrowser1.Document

If document IsNot Nothing Then
    If document.All("userName") IsNot Nothing Then
        If String.IsNullOrEmpty(document.All("userName").GetAttribute("value")) Then
            e.Cancel = True
            MsgBox("You must enter your name before you can navigate to " &
            e.Url.ToString())
        End If
    End If
End If
Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56
  • His real problem is the use of the `And` operator. I think this should have been addressed besides giving a different solution that ***isn't addressing his actual issue***. Also no need for all the nested if's because he isn't doing anything with them, it looks crowded.... – Trevor Jul 17 '16 at 05:47
  • @Zaggler - I disagree with your unsolicited moderation. My answer fixes his issue and is designed to do so with the minimum edits to the original code in order to allow the OP to see the problem. It's not the optimal answer as I am not totally rewriting his code. So your downvote is misplaced as the answer is not 'not useful' - it is just not as useful as you would like. If you actually ran his code you will find that my proposal fixes his problem within the scope of his current application. – Robin Mackenzie Jul 17 '16 at 05:56
  • I didnt say your solution doesn't work, it would indeed. His real issue is his use of the ternary operator... I would have explained that as it's clear he doesn't know... – Trevor Jul 17 '16 at 06:00
  • If you accept the solution works (but your criticism is that using nested IFs is sub-optimal compared to `AndAlso`) then your downvote is unwarranted. However, I accept your constructive criticism that there is a _better way to do what he wants_. Suggest you need to find the time to put yourself in the shoes of people with less capability than yourself. This will be more rewarding for you than playing thread-cop. IMO answers should be accessible to the questioners.... – Robin Mackenzie Jul 17 '16 at 06:03