C# NullException错误。帮个忙

C# NullException错误。帮个忙,c#,xna,argumentnullexception,C#,Xna,Argumentnullexception,有人能告诉我哪里出了问题吗?我一直收到错误消息: “ArgumentNullException未处理。此方法不接受null 用于此参数。参数名称:song“ 我找不到绕过它的办法 Song BGmusic; bool songstart = false; protected override void LoadContent() { currentgamescreen = Gamescreen.menuscreen; if (!songstart) {

有人能告诉我哪里出了问题吗?我一直收到错误消息:

“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");
}
歌曲音乐;
bool songstart=false;
受保护的覆盖void LoadContent()
{
currentgamescreen=Gamescreen.menuscreen;
如果(!songstart)
{
MediaPlayer.Play(音乐);
}
BGmusic=Game.Content.Load(“音频/摇滚乐”);
}

您可以调用
MediaPlayer.Play(bgmic)其中
BGmusic
尚未初始化,因此为空

可能是这样的:

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

    if (!songstart)
    {
        BGmusic = Game.Content.Load<Song>("audio/rockTheDragon");
        MediaPlayer.Play(BGmusic);
    }
 }
protected override void LoadContent()
{
currentgamescreen=Gamescreen.menuscreen;
如果(!songstart)
{
BGmusic=Game.Content.Load(“音频/摇滚乐”);
MediaPlayer.Play(音乐);
}
}

将解决问题。

在为
BGmusic
赋值之前,请先调用
MediaPlayer.Play(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);
    }
 }
歌曲音乐;
bool songstart=false;
受保护的覆盖void LoadContent()
{
currentgamescreen=Gamescreen.menuscreen;
BGmusic=Game.Content.Load(“音频/摇滚乐”);
如果(!songstart)
{
MediaPlayer.Play(音乐);
}
}
移动

 BGmusic = Game.Content.Load<Song>("audio/rockTheDragon");
BGmusic=Game.Content.Load(“音频/摇滚乐”);

在这个方法的顶部

BGmusic
在任何地方初始化的吗?这只是另一个改进:当你开始歌曲时,我会将bool
songstart
设置为true!在本代码的上下文中,当调用
MediaPlayer.Play
时,
BGmusic
显然为空。为什么不在播放文件之前检查文件是否需要加载或已经加载。你的逻辑毫无意义。这已经奏效了,非常感谢@Tigran。我知道这很简单,但我已经做了一整天了,我开始发疯了!当前正在处理:MediaPlayer.IsRepeating=true;