我遇到了一個問題,我收到一個「變量'strMap'值。空的引用異常可能導致運行時錯誤字符串strMap。「變量'strMap'在被賦值之前被使用,一個空引用異常可能在運行時產生」
據我所知,它是說我正在使用字符串之前,我正在應用一個值,但是,從代碼中我可以看到,我正在應用最後一組If if語句中的值(這些以前是If /其他人,但我認爲這是什麼與它搞亂。
結果,因爲strMap字符串作爲空事先它無法找到shell命令中指定的文件。
感謝。
Damon
Private Sub btnCreateServerSimple_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateServerSimple.Click
'Working code for server creation using pre-existing batch files
'Declarations
Dim strGameType As String
Dim strMap As String
Dim strServer As String
'Dim strBatchFile As String
Dim strBatchLoc As String
'Checking for selected server and setting strServer string accordingly
If cmbServerSimple.Text = "Server 1" Then
strServer = "server1"
strBatchLoc = frmInstallUpdateSettings.txtCSGOServer1BatchFilesLocation.Text
Else : cmbServerSimple.Text = "Server 2"
strServer = "server2"
strBatchLoc = frmInstallUpdateSettings.txtCSGOServer2BatchFilesLocation.Text
End If
'Checking for gametype and setting strGameType string accordingly
If cmbGameTypeSimple.Text = "Classic Competative" Then
strGameType = "classic_competative"
Else : cmbGameTypeSimple.Text = "Classic Casual"
strGameType = "classic_casual"
End If
'Checking for selected map and setting strMap string accordingly
If cmbMapSimple.Text = "de_aztec" Then
strMap = "de_aztec"
End If
If cmbMapSimple.Text = "de_dust2_se" Then
strMap = "de_dust2_se"
End If
If cmbMapSimple.Text = "de_dust" Then
strMap = "de_dust"
End If
If cmbMapSimple.Text = "de_inferno" Then
strMap = "de_inferno"
End If
If cmbMapSimple.Text = "de_nuke_ve" Then
strMap = "de_nuke_ve"
End If
If cmbMapSimple.Text = "de_mirage" Then
strMap = "de_mirage"
End If
If cmbMapSimple.Text = "de_train" Then
strMap = "de_train"
End If
If cmbMapSimple.Text = "de_vertigo" Then
strMap = "de_vertigo"
End If
Shell("C:\Users\Damon\Desktop\batchtest\" & strServer & "_" & strGameType & "_" & strMap & ".bat", AppWinStyle.NormalFocus)
End Sub
非常感謝您的回覆。我會改變這一點,再看看。我可能不得不回到你身邊。 –
我還可以問,當我的代碼的頂部聲明一個字符串將它的值設置爲無時,這是否應該是常見做法? –
是的,你應該儘可能初始化變量。如果沒有'=「''或'= String.Empty',變量的值就是'Nothing'(在Locals窗口中查看,或者在聲明後立即將鼠標懸停在運行時的變量上)。這會導致警告 - 如果/當傳遞Nothing或null時,預期字符串可能會導致異常。 – Plutonix