所以我使用C#在Visual Studio中的Windows窗體應用程序,我有三個數組:從陣列中播放聲音會返回一個錯誤 - C#,Visual Studio中
System.Media.SoundPlayer[] keyClick = new System.Media.SoundPlayer[4];
System.Media.SoundPlayer[] spaceClick = new System.Media.SoundPlayer[2];
System.Media.SoundPlayer[] enterClick = new System.Media.SoundPlayer[1];
,當程序初始化設置爲包含某些聲音文件:
private void textIn_Enter(object sender, EventArgs e)
{
for(int i = 0; i < 4; i++)
{
keyClick[i] = new System.Media.SoundPlayer(@"C:\Users\brain\OneDrive\Documents\Visual Studio 2015\Projects\TextAdventure\TextAdventure\Assets\keyClick" + i + ".wav");
}
for (int i = 0; i < 2; i++)
{
keyClick[i] = new System.Media.SoundPlayer(@"C:\Users\brain\OneDrive\Documents\Visual Studio 2015\Projects\TextAdventure\TextAdventure\Assets\spaceClick" + i + ".wav");
}
for (int i = 0; i < 1; i++)
{
keyClick[i] = new System.Media.SoundPlayer(@"C:\Users\brain\OneDrive\Documents\Visual Studio 2015\Projects\TextAdventure\TextAdventure\Assets\enterClick" + i + ".wav");
}
}
子現在,例如keyClick包含keyClick1.wav到keyClick5.wav。
keyClick[randomizer.Next(0, 4)].Play();
所以我在其中寫的第一個是上面的一個工作正常,但是我添加了一個命令,以使空格鍵:當按下一個鍵時,程序從一個特定的陣列起着在隨機索引的項從廣州雄兵陣列飾演:
spaceClick[randomizer.Next(0, 2)].Play();
然而,這也從enterClick陣列玩返回一個錯誤:
"An unhandled exception of type 'System.NullReferenceException' occurred in TextAdventure.exe
Additional information: Object reference not set to an instance of an object."
我不知道如何解決這個問題,就我所知它應該和keyClick一樣玩,但它不會。有誰知道如何解決這一問題?
丫太感謝你了,我無法相信我錯過了如此明顯的錯誤:) – TacoMakerMan