我在尝试执行代码时遇到此错误:
An unhandled exception of type 'System.NullReferenceException' occurred in [ApplicationName].exe
Additional information: Object reference not set to an instance of an object.
每次触发此操作时都会发生这种情况。这是代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Enabled = False
Button1.Text = "Checking..."
ToolStripStatusLabel1.Text = "Checking for devices..."
Dim ADB_Check_Result As Boolean = False
Dim Fastboot_Check_Result As Boolean = False
Dim ADB_Devices As New Process()
ADB_Devices.StartInfo.UseShellExecute = False
ADB_Devices.StartInfo.RedirectStandardOutput = True
ADB_Devices.StartInfo.CreateNoWindow = True
ADB_Devices.StartInfo.FileName = "adb.exe"
ADB_Devices.StartInfo.Arguments = "devices"
ADB_Devices.Start()
Dim ADB_Devices_Stream As IO.StreamReader = ADB_Devices.StandardOutput
Dim ADB_Devices_Output As String
While ADB_Devices_Stream.EndOfStream = False
ADB_Devices_Output = ADB_Devices_Stream.ReadToEnd()
End While
ADB_Devices.WaitForExit()
ADB_Devices.Close()
If ADB_Devices_Output.Contains(" device") Then
ToolStripStatusLabel1.Text = "Device found! [ADB]"
Button1.Enabled = True
ADB_Check_Result = True
Fastboot_Check_Result = False
Button1.Text = "Check for Devices"
Else
If ADB_Devices_Output.Contains("recovery") Then
ToolStripStatusLabel1.Text = "Device found! [Recovery Mode]"
Button1.Enabled = True
ADB_Check_Result = True
Fastboot_Check_Result = False
Button1.Text = "Check for Devices"
Else
If ADB_Check_Result = False Then
Dim Fastboot_Devices As New Process()
Fastboot_Devices.StartInfo.UseShellExecute = False
Fastboot_Devices.StartInfo.RedirectStandardOutput = True
Fastboot_Devices.StartInfo.CreateNoWindow = True
Fastboot_Devices.StartInfo.FileName = "fastboot.exe"
Fastboot_Devices.StartInfo.Arguments = "devices"
Fastboot_Devices.Start()
Dim Fastboot_Devices_Stream As IO.StreamReader = Fastboot_Devices.StandardOutput
Dim Fastboot_Devices_Output As String
While Fastboot_Devices_Stream.EndOfStream = False
Fastboot_Devices_Output = Fastboot_Devices_Stream.ReadToEnd()
End While
Fastboot_Devices.WaitForExit()
Fastboot_Devices.Close()
If Fastboot_Devices_Output.Contains("fastboot") Then
ToolStripStatusLabel1.Text = "Device found! [Fastboot Mode]"
Button1.Enabled = True
ADB_Check_Result = False
Fastboot_Check_Result = True
Button1.Text = "Check for Devices"
End If
End If
End If
End If
If Fastboot_Check_Result = False + ADB_Check_Result = False Then
ToolStripStatusLabel1.Text = "Device not found."
Button1.Enabled = True
ADB_Check_Result = False
Fastboot_Check_Result = False
Button1.Text = "Check for Devices"
End If
End Sub
这一行突出显示:
If Fastboot_Devices_Output.Contains("fastboot") Then
我尝试了一切,但我不知道如何解决这个问题:/
答案 0 :(得分:0)
您可以像这样检查NULL:
If Fastboot_Devices_Output IsNot Nothing AndAlso Fastboot_Devices_Output.Contains("fastboot") Then
ToolStripStatusLabel1.Text = "Device found! [Fastboot Mode]"
Button1.Enabled = True
ADB_Check_Result = False
Fastboot_Check_Result = True
Button1.Text = "Check for Devices"
End If
至于为什么你的字符串在if语句的位置为Null,请确保你的while循环没有将变量设置为Null:
While Fastboot_Devices_Stream.EndOfStream = False
Fastboot_Devices_Output = Fastboot_Devices_Stream.ReadToEnd()
End While