0

Alright, I'm having a really odd issues. I'm right now writing a simple game in C#/MonoGame (on Linux). I'm trying to play a SoundEffect. When I call Play() (even though it's been properly loaded in the LoadContent() method). It's throwing a NullReferenceException with the message Object Reference not set to an instance of an object.

Here is how the code is structured

public class MyGame : Game
{
    // ..
    private SoundEffect _sfx;

    public PingGame ()
    {
        // ...
    }

    protected override void Initialize ()
    {
        // ...
    }

    protected override void LoadContent ()
    {
        // ...

        // No errors here on loading it
        _sfx = Content.Load<SoundEffect>("noise.wav");
    }

    protected override void Update (GameTime gameTime)
    {
        // ...

        if (playSound)
        {
            // This is where the error is thrown
            _sfx.Play();
        }

        // ...
    }

    protected override void Draw (GameTime gameTime)
    {
        // ..
    }
}
Benjamin
  • 1,223
  • 1
  • 13
  • 22
  • Maybe you've run into [this issue](http://community.monogame.net/t/null-reference-exception-when-calling-play-on-a-soundeffect-or-soundeffectinstance/7319/10) – craftworkgames Aug 22 '16 at 04:14

3 Answers3

0

The error message is says it all. At the time when you invoke Update (GameTime gameTime) the object _sfx is not initialised.

It is not possible to know how you want design your game, but you can test this by changing the code as below, and you will not have the null reference exception anymore. That may not be how you want your code to be designed but it gives you an idea where it is going wrong and how to fix it. See code below.

protected override void Update (GameTime gameTime)
{
    // ...

    if (playSound)
    {
        // This is where the error is thrown
        // THIS ENSURES WHEN THIS METHOD IS INVOKED _sfx is initialized.

        _sfx = Content.Load<SoundEffect>("noise.wav");
        if(_sfx != null){
          _sfx.Play();
        }
    }
    // ...
}
Julius Depulla
  • 1,493
  • 1
  • 12
  • 27
  • 1
    would be better to test for `null` and if `_sfx == null` then load the sound, but yes. – Jean-François Fabre Aug 21 '16 at 21:32
  • @Jean-FrançoisFabre absolutely, null check added – Julius Depulla Aug 21 '16 at 21:33
  • The way that this code is structured, does this mean I need the load the sound effect every single time that that I want to play it? – Benjamin Aug 21 '16 at 21:37
  • But in XNA life-cycle LoadContent (which create instance of _sfx) is called before Update. I guess question is here why it was not called. – serhiyb Aug 21 '16 at 21:38
  • @serhiyb That's what I was wondering too. I've had other games structured like this where I load my sound in LoadContent(). I'm really perplexed by this. – Benjamin Aug 21 '16 at 21:38
  • @Benjamin can you include Initialize method code in your question, and constructor code as well please. – serhiyb Aug 21 '16 at 21:42
  • `protected override void LoadContent ()` means that it has to override some parent method right? It could not be a typo or a missing argument that would break virtual function calls? – Jean-François Fabre Aug 21 '16 at 21:44
  • @Benjamin, This just shows you a test why you are having a null reference exception and how to fix it, basically the object was not initialized. You can now go on and initialize the variable as you like. – Julius Depulla Aug 21 '16 at 22:20
  • I tried to move the Content.Load call to update when I want to play a sound effect. The same error is being thrown when `Play()` is called. I have no idea what is going on here... – Benjamin Aug 22 '16 at 01:37
  • @Benjamin The code snippet I wrote there will not throw a null reference exception but it may not also work if the file is not loaded. The file path or directory location to "noise.wav" may wrong and that will result in it not beig loaded. If you can post directory structure for noise.wav then we can help you by checking your file path is correct. What you have here says the code and file are in the same directory and I believe that may not be the case as most people put them in separate directories. – Julius Depulla Aug 22 '16 at 06:27
0

My blind guesses are (since you don't have code included):

  • GraphicsDeviceManager is not created inside of constructor (needs to be created before base.Initialize() is called)
  • Or you forget to call base.Initialize() method inside your Initialize method.
serhiyb
  • 4,753
  • 2
  • 15
  • 24
0
protected override void Update(GameTime gameTime)
{
    // ...

    if (playSound)
    {
        if (_sfx == null)
        {
            Content.Load<SoundEffect>("noise.wav");
        }
        _sfx.Play();
    }
}
Daniel G
  • 245
  • 4
  • 15