2017-04-06 72 views
1

我需要你的幫助。我已經使這個代碼從保存播放列表文件(.xml)加載,加載文件的作品,但它無法播放。無法播放保存播放列表文件(.xml)

播放時,從listbox1出現錯誤。我想SelectedIndex已經存在filePaths,但我不知道如何解決它?

這裏是我的代碼:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (listBox1.Items.Count > 1) 
     axWindowsMediaPlayer1.URL = filePaths[listBox1.SelectedIndex]; 
} 

Pic

+0

什麼錯誤,你現在得到些什麼?通過使用調試器,您可以輕鬆檢查'filePaths'中是否存在'SelectedIndex'。 – moondaisy

+0

axWindowsMediaPlayer1.URL = filePaths [listBox1.SelectedIndex];錯誤是「Nullreferenceexception was unhandled」 – Wesley

回答

0

既然你得到一個NullReferenceException was unhandled錯誤的問題可能是SelectedIndexnull。當你listBox1

嘗試檢查沒有選擇的項目會出現這種情況是在嘗試使用它之前:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (listBox1.Items.Count > 1 && listBox1.SelectedIndex != null) 
     axWindowsMediaPlayer1.URL = filePaths[listBox1.SelectedIndex]; 
} 
+0

謝謝你的回覆。但是這個問題在「axWindowsMediaPlayer1.URL = filePaths [listBox1.SelectedIndex];」中仍然是一樣的此代碼的黃色高線顯示警告消息「NullReferenceException未處理」 – Wesley

+0

嘗試調試以查看哪一個爲空。可能是因爲'axWindowsMediaPlayer1'或'filePaths'尚未初始化。 – moondaisy

+0

這是在filePaths [listBox1.SelectedIndex]; – Wesley