NullException错误。帮助一个菜鸟

时间:2013-01-25 14:52:21

标签: c# xna argumentnullexception

有人可以告诉我这里哪里出错吗?我一直收到错误消息:

  

" ArgumentNullException未处理。此方法不接受null   对于此参数。参数名称:song"

我无法找到解决方法。

Song BGmusic;
bool songstart = false;

protected override void LoadContent()
{
    currentgamescreen = Gamescreen.menuscreen;

    if (!songstart)
    {
        MediaPlayer.Play(BGmusic);
    }

    BGmusic = Game.Content.Load<Song>("audio/rockTheDragon");
}

3 个答案:

答案 0 :(得分:10)

你打电话给MediaPlayer.Play(BGmusic);,其中BGmusic尚未初始化,所以null。

Probabbly 让它像:

protected override void LoadContent()
{
    currentgamescreen = Gamescreen.menuscreen;

    if (!songstart)
    {
        BGmusic = Game.Content.Load<Song>("audio/rockTheDragon");
        MediaPlayer.Play(BGmusic);
    }
 }

将解决问题。

答案 1 :(得分:2)

在为MediaPlayer.Play(BGmusic)分配值之前,您正在调用BGmusic。请尝试以下方法:

Song BGmusic;
bool songstart = false;

protected override void LoadContent()
{
    currentgamescreen = Gamescreen.menuscreen;

    BGmusic = Game.Content.Load<Song>("audio/rockTheDragon");

    if (!songstart)
    {
        MediaPlayer.Play(BGmusic);
    }
 }

答案 2 :(得分:1)

移动

 BGmusic = Game.Content.Load<Song>("audio/rockTheDragon");

该方法的顶部