1

I'm getting this error when trying to execute my code:

An unhandled exception of type 'System.NullReferenceException' occurred in [ApplicationName].exe
Additional information: Object reference not set to an instance of an object.

This happens every time I trigger this action. This is the code:

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

And this line is highlighted:

If Fastboot_Devices_Output.Contains("fastboot") Then

I tried everything, but I don't know how to solve this problem :/

Dj_Mike238
  • 65
  • 1
  • 2
  • 7
  • 1
    Is it possible that your While loop is setting the variable to Null? Did you mean to do Fastboot_devices_output += instead of =? – Johnie Karr Nov 19 '14 at 22:25
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Nov 19 '14 at 22:36

1 Answers1

1

You can check for NULL like this:

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

As to why your string is Null at the point of this if statement, make sure your while loop isn't setting the variable to Null:

While Fastboot_Devices_Stream.EndOfStream = False
 Fastboot_Devices_Output = Fastboot_Devices_Stream.ReadToEnd()
End While
Johnie Karr
  • 2,744
  • 2
  • 35
  • 44
  • Thank you very much! It worked like a charm :) That While loop was actually setting the variable to Null, since when fastboot.exe doesn't detect any device, its result is Null :/ – Dj_Mike238 Nov 20 '14 at 15:07