这是我在一台机器上检测JDK安装的函数的代码。执行时的代码给出了处理的异常: -
第164号线是: - If ndpKey Is Nothing AndAlso ndpKey.GetValue("CurrentVersion") Is Nothing Then
Private Function HAS_JDK() As Boolean
Try
Const subkeyjava As String = "SOFTWARE\JavaSoft\Java Runtime Environment"
Using ndpKey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkeyjava)
If ndpKey Is Nothing AndAlso ndpKey.GetValue("CurrentVersion") Is Nothing Then
Close()
Return False
Else
Return True
End If
End Using
Catch e As NullReferenceException
MessageBox.Show("System Error Occured While Checking JDK !! ", "BLA BLA BLA - Error Occured !!")
MessageBox.Show(e.ToString())
Close()
Return -1
End Try
End Function
你的逻辑有点不正确。我认为你所追求的是OrElse
:
If ndpKey Is Nothing OrElse ndpKey.GetValue("CurrentVersion") Is Nothing Then
这检查ndpKey
或ndpKey.GetValue("CurrentVersion")
是否为空。如果第一次检查成功,它将不会继续第二次,因此没有例外。