Ich versuche, meine erste HTML-Benutzeroberfläche mit Webbrowser-Komponente in VB.Net zu erstellen. Ich habe diesen Code Beispiel auf Microsoft-WebsiteWebbrowser-Steuerelement löst NullReferenceException beim Navigieren
https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.document(v=vs.110).aspx gefunden:
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
Wenn ich es auf die Probe gestellt auf, die meiste Zeit wirft Ausnahme ‚System.NullReferenceException‘ in diesem Teil des Codes:
If document IsNot Nothing And
document.All("userName") IsNot Nothing And
String.IsNullOrEmpty(
document.All("userName").GetAttribute("value")) Then
Manchmal funktioniert es, aber meistens funktioniert es überhaupt nicht. Irgendeine Idee, wie man das repariert? Ich bin sehr neu in .Net-Plattform und tut mir leid, wenn es Fehlbuchstabierungen gibt. Jede Hilfe wird geschätzt.
ändern 'und' zu' AndAlso' ... Es ist ein Kurzschluss ... – Codexer
Mögliche Duplikat [Was für eine Nullreferenceexception ist, und wie kann ich es beheben ?] (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Codexer