I have a basic sound manager class that’s derived from an online example: http://www.xnawiki.com/index.php?title=Basic_Sound_Manager_For_XNA_GS_3.0
The problem is that whenever I call the LoadSound(string assetName, string assetPath) method, I get a NullReferenceException. I’m guessing this means the content manager couldn’t find the reference specified and is returning null.
It doesn’t make sense to me though, as there are definitely sounds in the content folder I’m referencing. It worked before I moved sound management to a separate class.
Here’s the relevant code snippet for my SoundManager class:
public static class SoundManager
{
// Class variables
static Dictionary<string, SoundEffect> sounds = new Dictionary<string,
SoundEffect>();
static Song currentSong;
static ContentManager content;
static float soundVolume = 1f;
// Initialize: Load the game's content manager.
public static void Initialize(Game game)
{
content = game.Content;
}
public static void LoadSound(string assetName)
{
LoadSound(assetName, assetName);
}
// Load a sound from a passed name into the dictionary
public static void LoadSound(string assetName, string assetPath)
{
sounds.Add(assetName, content.Load<SoundEffect>(assetPath));
}
}
The exception occurs in the line
sounds.Add(assetName, content.Load<SoundEffect>(assetPath));
In Game1’s Initialize() method, I’m calling:
SoundManager.Initialize(this);
In Game1’s LoadContent() method, I’m calling the following:
SoundManager.LoadSound("collision", "Sounds\\collision");
The Content.RootDirectory is “Content”, and a sound exists at content/sounds/ding.wav (asset name collision). I seriously can’t see any problems here. If there are any weird scope problems anyone can see, please help, I have an impending deadline.
If the NRE is in your code, then it’s got to be that
contentis null: i.e.,Initializeis not being called beforeLoadSound. Use a breakpoint or some quick debugging to check this is the case.