2016-07-17 3 views
-1

VB.Net에서 Webbrowser 구성 요소로 첫 HTML UI를 작성하려고합니다. 나는 마이크로 소프트 사이트에이 코드 예제탐색 할 때 웹 브라우저 컨트롤에서 NullReferenceException을 throw합니다.

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 

을, 대부분의 시간은 코드의이 부분에서 예외 'System.NullReferenceException'가 발생합니다 :

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

때때로 작동하지만 대부분 작동하지 않습니다. 이 문제를 어떻게 해결할 수 있습니까? 나는 .Net 플랫폼에 매우 익숙하다. 철자가 틀린다면 유감스럽게 생각한다. 어떤 도움을 주셔서 감사합니다.

+0

을 그 단락 ... [NullReferenceException이, 그리고 내가 어떻게 해결합니까 무엇의 – Codexer

+0

가능한 중복 ?] (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Codexer

답변

-1

document이 아무것도 아닌 경우 documentNothing 인 동안 등록 정보에 액세스하려고 시도하기 때문에문의 다른 절에 예외가 발생합니다. 이 같은 코드를 다시 작성할 필요 :

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 
이`And`에`AndAlso` ... 변경
+0

그의 진짜 문제는'And' 연산자의 사용입니다. 나는 이것이 그의 실제 문제 ***를 다루지 않고있는 다른 해결책을 제시하는 것 이외에 해결되어야한다고 생각한다. 또한 그 (것)들로 무엇이든을하고 있지 않기 때문에 모든 중첩 된 if를위한 필요 없음, 그것은 붐비는 것처럼 보입니다 .... – Codexer

+0

@Zaggler - 나는 너의 자발적인 중용에 동의하지 않는다. 내 답변은 문제를 해결하고 운영자가 문제를 볼 수 있도록 원본 코드의 최소 수정 사항으로 해결하도록 설계되었습니다. 제가 코드를 완전히 다시 쓰고있는 것은 아니므로 최적의 대답은 아닙니다. 따라서 응답이 '유용하지 않음'이 아니므로 다운 보트가 잘못 배치됩니다. 원하는대로 유용하지 않습니다. 코드를 실제로 실행했다면 내 제안이 현재의 응용 프로그램 범위 내에서 문제를 해결한다는 것을 알게 될 것입니다. –

+0

나는 당신의 해결책이 효과가 없다고 말하지 않았다. 그의 진짜 문제는 삼항 연산자를 사용하는 것입니다 ... 나는 분명히 그가 알지 못한다고 설명했을 것입니다 ... – Codexer

관련 문제