-1

As I'm currently making a 2D game, I'm trying to make it possible for my character to load between levels but I'm having a bit of an issue. The first level that my player starts on is loaded in an array with no issue at all in the Game1 class like this:

secondLevel secondLev;  // instance for secondLevel class
Map map;                // instance for Map class


secondLev = new secondLevel(); // used in the Initialize() function

protected override void LoadContent()
{
    map.Generate(new int[,] {

    // 0 = no tile drawn
    // 3 = tile is drawn

    {0,0,0,0,0,0,0,0,0,0,},
    {0,0,0,0,0,0,0,0,0,0,},
    {0,0,0,0,0,0,0,0,0,0,},
    {0,0,0,0,0,0,0,0,0,0,},
    {3,3,3,3,3,3,3,3,3,3,},
    {3,3,3,3,3,3,3,3,3,3,},

    }, 57); // size 
}

So to be able to load the second level, I have attempted to make a new class called secondLevel that holds the new level array and simply loads the array in its Load() function, like so:

class secondLevel
{
    Map map;

        public void Initialize()
        {
           map = new Map();
        }

        public void Load()
        {
           map.Generate(new int[,] {

            // 0 = no tile drawn
            // 3 = tile is drawn

            {0,0,0,0,0,0,0,0,0,0,},
            {0,0,0,0,0,0,0,0,0,0,},
            {3,3,3,3,3,3,3,3,3,3,},
            {3,3,3,3,3,3,3,3,3,3,},
            {3,3,3,3,3,3,3,3,3,3,},
            {3,3,3,3,3,3,3,3,3,3,},

            }, 57); // size 
          }
       }

Now in my Game1 class, I have placed an if statement that checks if the player has collided with the spike that loads the next level, like so:

if (player.Bounds.Intersects(spike1.Bounds)) // if player intersects with spike
{
    secondLev.Load();
}

But when my player intersects with the spike, my game freezes and I get the error message: Object reference not set to an instance of an object.

What is my issue?

If I'm missing any additional code to my question that could help you fix this, please let me know!

toadflax
  • 375
  • 4
  • 17
  • 2
    The error is most likely not in the code you posted, we would need more code to help you solve this. Also, someone is bound to vote to close this question soon as a duplicate of https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it so you may want to read up on that a while. – Bradley Uffner May 19 '16 at 17:19
  • You may be putting the cart before the horse here. I recommend you either 1) do some tutorials or read a book to understand the basic workings of C# or 2) get a book that lets you learn C# through the process of game-making before you just venture out on your own with such a large undertaking. – paste May 22 '16 at 16:04
  • @paste, that's completely irrelevant advice to give, especially when one of the answers below solved my issue just fine. – toadflax May 22 '16 at 22:20
  • @TimonMaths It's not irrelevant since I know that you've asked a number of questions recently on SO that are easily understood by using any of a plethora of resources that introduce you to and walk you through C# programming so you can avoid the issues that you've encountered. Not to mention, these books will get you an answer quicker than waiting for replies, and it will help you build the skills necessary to solve your problems on your own. – paste May 22 '16 at 23:03

2 Answers2

0

Due to lack of context, it is hard to tell what exactly caused it. However, if the exception were to happen within the code that you presented, one of the following might be not be initialized : secondLev or secondLev.map.

if (player.Bounds.Intersects(spike1.Bounds)) // if player intersects with spike
{
    secondLev = new secondLevel();
    secondLev.Initialize();
    secondLev.Load();
}

If that doesn't solve the problem, you should look at the stacktrace for clue.

Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
0

Your secondLevel class is missing a constructor. That means that the Load() and Initialize() methods are never called. To fix this error, add a constructor that calls those methods.

Example:

public secondLevel(){
this.Initialize();
this.Load();
}

Another thing: you shouldn't be making a whole new class for each new level. Instead, you should have a Level struct (or class if you really want) that has all the properties of a Level. Then, to make new levels, just do Level secondLevel = new Level(map,size);

Epic
  • 61
  • 1
  • 4