2015-10-07 34 views
1
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    Dictionary<string, LivePacketDevice> copyOfAdapters = find_adapter.Adapters; 
    copyOfAdapters.Remove(comboBox1.SelectedText); 
    Dictionary<string, LivePacketDevice> copyOfAdapters22 = copyOfAdapters; 

} 

啓動項目後,exeption被扔的NullReferenceException在comboBox_SelectedIndexChanged

型 'System.NullReferenceException' 的例外發生在......但是在用戶代碼中沒有處理

附加信息:未將對象引用設置爲對象的實例。

在第一行方法結束時。但是find_adapter是在Form的構造函數中創建的。那麼錯誤在哪裏? 我真的不知道:/ THX

+0

什麼是你的IDE告訴你的是空項目,如果你設置一個斷點? –

+0

find_adapter爲空....但爲什麼在運行programm後立即拋出此問題,並且在單擊combobox後沒有? –

+0

在調用此代碼之前,您顯然沒有將find_adapter設置爲任何東西(除null之外)... –

回答

0

我想你應該對空值增加檢查,並嘗試使用comboBox1.SelectedItem.ToString()代替comboBox1.SelectedText

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (find_adapter.Adapters == null || !find_adapter.Adapters.Any()) 
     { 
      Dictionary<string, LivePacketDevice> copyOfAdapters = find_adapter.Adapters; 
      copyOfAdapters.Remove(comboBox1.SelectedItem.ToString()); 
      var copyOfAdapters22 = new Dictionary<string, LivePacketDevice>(copyOfAdapter); 
     } 
    } 
+1

感謝您所有的工作 –

+0

很高興能有所幫助 – Marusyk

+0

我無法在90分鐘內創建另一個問題。我能問你聽到嗎? ...這是關於這種方法,並從字典中刪除。 –

相關問題