Question doesn't return a value on all code paths

cybertank378

New member
Joined
Jul 10, 2012
Messages
3
Programming Experience
Beginner
Private Function GetGTAProc() As Process
Dim Procs() As Process
Procs = Process.GetProcesses
For Each proc As Process In Procs
If proc.ProcessName.Equals("gta_sa") Then
Return proc
End If
Next
End Function

Warning 1 Function 'GetGTAProc' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. D:\Projects\Source\gta_launcher\trunk\idgssamplauncher\frm_main.vb 45 5 idgssamplauncher

can you help me ?
 
The warning is there because your function may not reach the Return statement and thus implicitly return default value Nothing. Since it is implicit it is assumed that you may not be aware of that possibility, and may have missed some code logic. If you want to get rid of that warning explicitly return Nothing if your process is not found. Some advocate for this reason that there should only be one Return statement and that it should be the last statement of a function, but I would happily include several Return statements in a function if that would suit better.
 
I personally prefer assignments and Exit Function to Return, just because I find it reads better usually.

Private Function GetGTAProc() As Process
    GetGTAProc = Nothing
    
    Dim Procs() As Process = Process.GetProcesses
    For Each proc As Process In Procs
        If proc.ProcessName.Equals("gta_sa") Then
            GetGTAProc = proc
            Exit Function
        End If
    Next
End Function
 
I personally prefer assignments and Exit Function to Return, just because I find it reads better usually.

Private Function GetGTAProc() As Process
    GetGTAProc = Nothing
    
    Dim Procs() As Process = Process.GetProcesses
    For Each proc As Process In Procs
        If proc.ProcessName.Equals("gta_sa") Then
            GetGTAProc = proc
            Exit Function
        End If
    Next
End Function


Ok herman thanks for your info now i had resolve the problem sorry im beginner in here

now i had new problem

Untitled3.png

but i can find the error result
 
Last edited:
Show the part of the code that calls GetGTAProc.

is ok dude i resolved that but i had a new trouble
after all proceed are finished
i want to kill my app and my game on same time


My next error
Cannot process request because the process ('PID') has exited.

    Private Sub Quit()
        lbl_status.Text = "Exiting..."
        Try
            If IsRunning("gta_sa") Then GetGTAProc().Close()
            For Each Datafile In files.GetFiles
                If InStr(Datafile.Name, ".bscript") > 0 Then
                    My.Computer.FileSystem.RenameFile(Datafile.FullName, Path.ChangeExtension(Datafile.Name, ".asi"))
                End If
                If InStr(Datafile.Name, "d3d") Then
                    If Path.GetExtension(Datafile.Name) = ".bdls" Then
                        My.Computer.FileSystem.RenameFile(Datafile.FullName, Path.ChangeExtension(Datafile.Name, ".dll"))
                    End If
                End If
            Next
            Me.Close()
            Application.Exit()
        Catch exc As Exception
            Me.Close()
            Application.Exit()
        End Try
    End Sub
 
Back
Top