0

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");
}
user1306322
  • 8,561
  • 18
  • 61
  • 122
Jon C
  • 69
  • 1
  • 1
  • 6
  • 1
    is `BGmusic` initialized anywhere? – SWeko Jan 25 '13 at 14:53
  • 1
    Just another improvement: I'd set the bool `songstart` to true as well when you start the song! – 2pietjuh2 Jan 25 '13 at 14:57
  • Its clear in the context of this code that `BGmusic` is null when `MediaPlayer.Play` is called. Why are you not checking to see if the file needs to be loaded or has already been loaded before playing the file. Your logic makes zero sense. – Security Hound Jan 25 '13 at 15:42

3 Answers3

10

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.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • This has worked, thanks very much @Tigran. Knew it would be something simple, but I've been at it all day and I'm starting to go crazy! Currently working on: MediaPlayer.IsRepeating = true; – Jon C Jan 25 '13 at 15:00
2

You're calling MediaPlayer.Play(BGmusic) before assigning a value to BGmusic. Try the following:

Song BGmusic;
bool songstart = false;

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

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

    if (!songstart)
    {
        MediaPlayer.Play(BGmusic);
    }
 }
JosephHirn
  • 720
  • 3
  • 9
1

Move

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

To the top of that method