Search code examples
c#xnaargumentnullexception

NullException error. Help a noob out


Can someone please tell me where I am going wrong here? I keep receiving the error message:

"ArgumentNullException was unhandled. This method does not accept null for this parameter. Parameter name: song"

I cannot find a way around it.

Song BGmusic;
bool songstart = false;

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

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

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

Solution

  • Well you call MediaPlayer.Play(BGmusic); where BGmusic not yet intialized, so null.

    Probabbly making it like:

    protected override void LoadContent()
    {
        currentgamescreen = Gamescreen.menuscreen;
    
        if (!songstart)
        {
            BGmusic = Game.Content.Load<Song>("audio/rockTheDragon");
            MediaPlayer.Play(BGmusic);
        }
     }
    

    will resolve a problem.