2013-10-20 23 views
1

我遇到了一個問題,我收到一個「變量'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 

回答

3

它都在談論這一點:

Dim strMap As String 

將其更改爲:

Dim strMap As String = "" 

你是正確的,它被設置前使用,但該警告是告訴你IF你試過在塊中使用它之前,它可能會導致異常。其他字符串也應該設置。

+0

非常感謝您的回覆。我會改變這一點,再看看。我可能不得不回到你身邊。 –

+0

我還可以問,當我的代碼的頂部聲明一個字符串將它的值設置爲無時,這是否應該是常見做法? –

+1

是的,你應該儘可能初始化變量。如果沒有'=「''或'= String.Empty',變量的值就是'Nothing'(在Locals窗口中查看,或者在聲明後立即將鼠標懸停在運行時的變量上)。這會導致警告 - 如果/當傳遞Nothing或null時,預期字符串可能會導致異常。 – Plutonix

1

如果你對cmbMapSimple組合的所有測試都失敗了,那麼你的strMap沒有初始化,編譯器會正確地提醒你。

你可以避開明確值分配給您的變量的問題,當你把它聲明

Dim strMap As String = String.Empty 

然後,當然,你決定做的,如果變量爲空是什麼到你的代碼。

+0

非常感謝,這與Plutonix的回答很相似,所以我也會給出一個答案,看看它是如何發展的。再次感謝。 –

1

有可能strMap沒有設置任何東西。 最好的解決方案是將所有If Else語句更改爲Select Case語句,然後使用Case Else將strMap設置爲「unknown」

+0

謝謝馬特。 Hans Passant爲這個確切的理論提供了一套代碼集,我會給出這個答案。 –

2

警告是準確的,如果If()語句都不匹配,最後strMap將被取消分配。編寫更好的代碼,使用If/ElseIf/Else。 Select語句應該是自己的喜好在這裏:

Select Case cmbMapSimple.Text 
     Case "de_aztec" : strMap = "de_aztec" 
     Case "de_dust2_se" : strMap = "de_dust2_se" 
     Case "de_dust"  : strMap = "de_dust" 
     '' etc... 
     Case Else : Throw New Exception("Invalid map selection") 
    End Select 

這也有助於你到達最有可能正確的代碼,因爲你已經確信組合框總是由它的DropDownStyle屬性設置爲DropDownList有一個有效的選擇:

If cmbMapSimple.SelectedIndex < 0 Then 
     Throw New Exception("Invalid map selection") 
    End If 
    strMap = cmbMapSimple.Text 
+0

非常感謝Hans,我從來沒有在我的代碼中使用過異常/錯誤處理,因爲我是新手,所以我會在我的開發副本中進行一些嘗試。我不會說謊,我雖然你的回答是關於SQL情況下乍一看時的陳述。我甚至沒有意識到你可以在VB.net中做到這一點。 –

+0

這與SQL沒有任何關係,Select Case語句是純VB.NET語法。 –

+0

我明白漢斯,我只是在評論我最初的想法! –

相關問題