2012-01-11 4 views
-1

, 나는 서비스가 다음 코드를 사용하여 TS에서 실행중인 검색하기 위해 노력하고 사전에 터미널 서버에서 실행중인 서비스를 추가 할 때 :Null 참조 문제 VB.NET에서

Imports System.ServiceProcess 
... 
Dim dictservice As Generic.Dictionary(Of String, Services) 

Public Sub GetService() 
    Dim localServices As ServiceController() = ServiceController.GetServices() 
    For Each service As ServiceController In localServices 
     dictservice.Add(service.DisplayName, New Services(service.DisplayName, service.ServiceName, service.Status.ToString)) 
    Next 
End Sub 

내 서비스 클래스입니다 다음과 같이

Class Services 
    Private _displayName As String 
    Private _serviceName As String 
    Private _serviceStatus As String 

    Sub New(ByVal DisplayName As String, ByVal ServiceName As Object, ByVal ServiceStatus As String) 
     _displayName = DisplayName 
     _serviceName = ServiceName 
     _serviceStatus = ServiceStatus 
    End Sub 

    Public Overrides Function ToString() As String 
     Return _serviceStatus 
    End Function 
End Class 

내가 그것을 사전 표시 이름 채울 것으로 보인다 디버그 모드를 통해 단계 : 응용 프로그램 환경 서비스 이름 : AElookUpSVC 서비스 상태를 : 실행을

Null reference exception was unhandled: Object reference not set to an instance of an object.

가 널 참조를 찾는 어디 내 인생 해결할 수 없기 : (4)

이 때 내가 다음과 같은 오류가 다음 항목에 이동하려고?

+0

미래에는 새로운 질문을 다시하지 말고 기존 질문에 새 정보를 편집하십시오. 감사. – Kev

+1

질문을 삭제하면 다른 사람들이 답변을 해석하는 것이 훨씬 어려워집니다 ... – ForeverWintr

답변

4

당신은 New하여 사전을 초기화해야합니다

Dim dictservice As New Generic.Dictionary(Of String, Services) 

Public Sub GetService() 
    Dim localServices As ServiceController() = ServiceController.GetServices() 
    For Each service As ServiceController In localServices 
     dictservice.Add(service.DisplayName, New Services(service.DisplayName, service.ServiceName, service.Status.ToString)) 
    Next 
End Sub

을 지금은 따라서 NullReferenceExceptionNothing입니다.

1

가장 큰 문제는 dictServiceNothing이고 따라서 NullReferenceException이되는 것 같습니다. dictService 초기화 및 선언은 어디에서합니까? 초기화가이 메서드보다 먼저 발생하는지 확인하고 있습니까?

이것이 문제가 아닌 경우 디버거를 연결하여 실행 해 보셨습니까? 그것은 실패하고있는 행을 가리켜 야합니다.