0

I am trying to insert a sound whenever there is a match in my game. However, I keep getting a NullReferenceException when I get the match. It goes to the line soundEffectInstance.Play();. Any help is greatly appreciated. I've attempted everything when trying to get it to work. Here is my code:

using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;

namespace AndyKlax.KlaxGame
{
internal class Grid
{
    private const int GridOrigin = 660;


    SoundEffect soundEffect;
    SoundEffectInstance soundEffectInstance;


    private readonly List<Block>[] _grid = new List<Block>[Klax.NumberOfColumns];

    public void LoadContent(ContentManager content)
    {

        soundEffect = content.Load<SoundEffect>(@"Sounds/24372^pop2.mp3");
        soundEffectInstance = soundEffect.CreateInstance();

    }

    internal Grid()
    {
        for (int i = 0; i < Klax.NumberOfColumns; i++ )
        {
            _grid[i] = new List<Block>();
        }
    }

    private bool RemoveBlocks()
    {
        var foundBlocksToRemove = false;

        //Mark all blocks in the grid as unmatched
        for (int column = 0; column < Klax.NumberOfColumns; column++)
        {
            for (int row =_grid[column].Count -1 ; row >=0 ; row--) //Loop backwards do its safe to remove
            {
                if (_grid[column][row].Delete)
                {
                    _grid[column].RemoveAt(row);
                    foundBlocksToRemove = true;
                }
            }
        }

        return foundBlocksToRemove;
    }



    private void SearchForMatches()
    {

        //Search the whole grid - look for matches in each direction and set the deleted flag for anything you find
        for (int column = 0; column < Klax.NumberOfColumns; column++)
        {
            for (int row = 0; row < _grid[column].Count; row++)
            {
                SearchForMatches(column, row, 1, 0);  //Horizontal
                SearchForMatches(column, row, 0, 1);  //Vertical
                SearchForMatches(column, row, 1, 1);  //Diagonal one way
                SearchForMatches(column, row, 1, -1);  //Diagonal the other way
            }
        }
    }

    private void SearchForMatches(int column, int row, int xStep, int yStep)
    {


        int startColorIndex = _grid[column][row].ColorIndex;
        int matchCount = 0;

        int x = column;
        int y = row;

        //Loop in the given direction till we run out of color or grid
        do
        {
            matchCount++;
            x += xStep;
            y += yStep;

        } while (x < Klax.NumberOfColumns && y < _grid[x].Count && y>0 && startColorIndex == _grid[x][y].ColorIndex);

        //If the match is long enough 
        if (matchCount >= 3)
        {
            //Then mark all those blocks for removal
            x = column;
            y = row;

            //Loop in the given direction till we run out of color or grid
            do
            {
                _grid[x][y].Delete = true;

                x += xStep;
                y += yStep;
                soundEffectInstance.Play();

            } while (x < Klax.NumberOfColumns && y < _grid[x].Count && startColorIndex == _grid[x][y].ColorIndex);
        }
    }

    private void ResetDeleteFlags()
    {
        //Mark all blocks in the grid as unmatched
        for (int column = 0; column < Klax.NumberOfColumns; column++)
        {
            for (int row = 0; row < _grid[column].Count; row++)
            {
                _grid[column][row].Delete = false;
            }
        }
    }

    internal void Draw(SpriteBatch spriteBatch)
    {
        //Like the paddle the position of the block is inferred by its position in the grid
        spriteBatch.Begin();
        for (int column = 0; column < Klax.NumberOfColumns; column++)
        {
            for (int row = 0; row < _grid[column].Count; row++)
            {
                _grid[column][row].Draw(spriteBatch, column, GridOrigin - row * Klax.Texture.Height);
            }
        }
        spriteBatch.End();
    }

    internal void Add(int column, Block block)
    {
        _grid[column].Add(block);

        //Search for matches
        do
        {
            ResetDeleteFlags();  
            SearchForMatches();  //Look for matches and mark their deleted flag
        } while (RemoveBlocks());  //If we removed any then iterate again as there may be new matches

    }

    public bool HasSpace(int column)
    {
        //Is there room in this column
        return _grid[column].Count < Klax.NumberOfRows;
    }
}

}

  • What language is that, please add that to your tag – Rafael Jan 28 '15 at 06:31
  • 1
    Sorry. New to all this. It's my first time posting. It's C# – Jessica Ervin Jan 28 '15 at 06:37
  • I think the error message is quite obvious, `soundEffectInstance` is null. You might want to check why `soundEffect.CreateInstance()` is returning null, I believe the line above that (`content.Load....`) is the reason. – Dan Jan 28 '15 at 06:41
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Yuval Itzchakov Jan 28 '15 at 06:45
  • I understand what the error message means. It means that for some reason it is not loading the audio into the instance. What I can't figure out is why it's not being loaded. I've looked at and changed everything I can think of on the content.Load line that I thought might be causing the problem (BTW in my original code and in the code that I'm now using the .mp3 is not on the source) – Jessica Ervin Jan 28 '15 at 06:49
  • Then you might want to target your question at the library instead of `C#` and `NullReferenceException`, since the problem is originating from the Audio library. You will get more relevant answers that way. – Dan Jan 28 '15 at 07:01

1 Answers1

0

From what I'm seeing in your code you are using ContentManager.Load from the Microsoft.Xna.Framework framework.

Under this assumption your code probably won't ever work and always will return null.

The problem here is twofold:

  1. ContentManager.Load supports only specific types as given by its definition (I'll post a link further below). SoundEffects are not part of these types

  2. This load method thinks that all files given have no extension by themselves but instead have an ungiven .xnb extension (and are of the xnb format).

Thus your Sounds/24372^pop2.mp3 translates to Sounds/24372^pop2.mp3.xnb for it.

One example directly from the documentation how it is used:

Model model = contentManager.Load<Model>( ".\\content\\models\\box" );

This would load box.xnb as a model.

The full documentation for that method is here: https://msdn.microsoft.com/en-us/library/bb197848.aspx

Like I mentioned if my assumption that you are using THAT method is correct then it is clear why the loading fails with that as it is looking for a file that does not exist. But even if that is corrected according to the documentation it should still not work as it is not of a supported type (I find it strange though that the method does not throw an exception when you try to use it to load something not supported but that developer oversight has nothing to do with your problem at all in essence).

Thomas
  • 2,886
  • 3
  • 34
  • 78