Object reference not set to an instance of an object.

I have some classes I am writing for a simple board game.

I do not think my gamePiece array is getting initialized correctly and I'm not sure what is needed:

The exception is happening HERE:

public void InitNewGame(string player1, string player2)
            {
                  gamePieceArray[0,0].AssignAsNewMasterRobot(player1);  <<<<EXCEPTION
...
...
...
...
...
}






SOURCE CODE:
=======================================
Here is my code for the Main Form:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace robotz
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
            public GameWorld gw = new GameWorld();

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(856, 621);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.Load += new System.EventHandler(this.Form1_Load);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
//                  for(int i = 0; i < 10;i++)
//                  {
//                        for(int j = 0;j<10;j++)
//                        {
//                              System.Diagnostics.Debug.WriteLine("gamePieceArray[" + i.ToString() + "," + j.ToString() + "].AssignAsNewEmpty();");
//                        }
//                  }

                  gw.InitNewGame("tom","bob");
                  gw.DisplayGame();
            }
      }
}





Here is my code for the Game World class:

using System;

namespace robotz
{
      /// <summary>
      /// Summary description for GameWorld.
      /// </summary>
      public class GameWorld
      {
            //[Row,Col]
            public GamePiece[,] gamePieceArray = new GamePiece[10,10];

            public GameWorld()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }
            public void DisplayGame()
            {
                  for(int i=0;i<10;i++)
                  {
                        for(int j=0;j<10;j++)
                        {
                              System.Diagnostics.Debug.WriteLine(gamePieceArray[i,j].DisplayImage());
                        }
                  }
                  
            }

            public void InitNewGame(string player1, string player2)
            {
                  gamePieceArray[0,0].AssignAsNewMasterRobot(player1);
                  gamePieceArray[0,1].AssignAsNewEmpty();
                  gamePieceArray[0,2].AssignAsNewMedic(player1);
                  gamePieceArray[0,3].AssignAsNewEmpty();
                  gamePieceArray[0,4].AssignAsNewEmpty();
                  gamePieceArray[0,5].AssignAsNewEmpty();
                  gamePieceArray[0,6].AssignAsNewEmpty();
                  gamePieceArray[0,7].AssignAsNewEmpty();
                  gamePieceArray[0,8].AssignAsNewEmpty();
                  gamePieceArray[0,9].AssignAsNewEmpty();
                  gamePieceArray[1,0].AssignAsNewEmpty();
                  gamePieceArray[1,1].AssignAsNewMedic(player1);
                  gamePieceArray[1,2].AssignAsNewDestroyer(player1);
                  gamePieceArray[1,3].AssignAsNewEngineer(player1);
                  gamePieceArray[1,4].AssignAsNewEmpty();
                  gamePieceArray[1,5].AssignAsNewEmpty();
                  gamePieceArray[1,6].AssignAsNewEmpty();
                  gamePieceArray[1,7].AssignAsNewEmpty();
                  gamePieceArray[1,8].AssignAsNewWall();
                  gamePieceArray[1,9].AssignAsNewEmpty();
                  gamePieceArray[2,0].AssignAsNewMedic(player1);
                  gamePieceArray[2,1].AssignAsNewDestroyer(player1);
                  gamePieceArray[2,2].AssignAsNewEmpty();
                  gamePieceArray[2,3].AssignAsNewEmpty();
                  gamePieceArray[2,4].AssignAsNewEmpty();
                  gamePieceArray[2,5].AssignAsNewEmpty();
                  gamePieceArray[2,6].AssignAsNewEmpty();
                  gamePieceArray[2,7].AssignAsNewEmpty();
                  gamePieceArray[2,8].AssignAsNewEmpty();
                  gamePieceArray[2,9].AssignAsNewEmpty();
                  gamePieceArray[3,0].AssignAsNewEmpty();
                  gamePieceArray[3,1].AssignAsNewEmpty();
                  gamePieceArray[3,2].AssignAsNewEmpty();
                  gamePieceArray[3,3].AssignAsNewEmpty();
                  gamePieceArray[3,4].AssignAsNewEmpty();
                  gamePieceArray[3,5].AssignAsNewEmpty();
                  gamePieceArray[3,6].AssignAsNewEmpty();
                  gamePieceArray[3,7].AssignAsNewEmpty();
                  gamePieceArray[3,8].AssignAsNewEmpty();
                  gamePieceArray[3,9].AssignAsNewEmpty();
                  gamePieceArray[4,0].AssignAsNewEmpty();
                  gamePieceArray[4,1].AssignAsNewEmpty();
                  gamePieceArray[4,2].AssignAsNewEmpty();
                  gamePieceArray[4,3].AssignAsNewEmpty();
                  gamePieceArray[4,4].AssignAsNewEmpty();
                  gamePieceArray[4,5].AssignAsNewEmpty();
                  gamePieceArray[4,6].AssignAsNewEmpty();
                  gamePieceArray[4,7].AssignAsNewEmpty();
                  gamePieceArray[4,8].AssignAsNewEmpty();
                  gamePieceArray[4,9].AssignAsNewEmpty();
                  gamePieceArray[5,0].AssignAsNewEmpty();
                  gamePieceArray[5,1].AssignAsNewEmpty();
                  gamePieceArray[5,2].AssignAsNewEmpty();
                  gamePieceArray[5,3].AssignAsNewEmpty();
                  gamePieceArray[5,4].AssignAsNewEmpty();
                  gamePieceArray[5,5].AssignAsNewEmpty();
                  gamePieceArray[5,6].AssignAsNewEmpty();
                  gamePieceArray[5,7].AssignAsNewEmpty();
                  gamePieceArray[5,8].AssignAsNewEmpty();
                  gamePieceArray[5,9].AssignAsNewEmpty();
                  gamePieceArray[6,0].AssignAsNewEmpty();
                  gamePieceArray[6,1].AssignAsNewEmpty();
                  gamePieceArray[6,2].AssignAsNewEmpty();
                  gamePieceArray[6,3].AssignAsNewEmpty();
                  gamePieceArray[6,4].AssignAsNewEmpty();
                  gamePieceArray[6,5].AssignAsNewEmpty();
                  gamePieceArray[6,6].AssignAsNewEmpty();
                  gamePieceArray[6,7].AssignAsNewEmpty();
                  gamePieceArray[6,8].AssignAsNewEmpty();
                  gamePieceArray[6,9].AssignAsNewEmpty();
                  gamePieceArray[7,0].AssignAsNewEmpty();
                  gamePieceArray[7,1].AssignAsNewEmpty();
                  gamePieceArray[7,2].AssignAsNewEmpty();
                  gamePieceArray[7,3].AssignAsNewEmpty();
                  gamePieceArray[7,4].AssignAsNewEmpty();
                  gamePieceArray[7,5].AssignAsNewEmpty();
                  gamePieceArray[7,6].AssignAsNewEmpty();
                  gamePieceArray[7,7].AssignAsNewEmpty();
                  gamePieceArray[7,8].AssignAsNewEmpty();
                  gamePieceArray[7,9].AssignAsNewEmpty();
                  gamePieceArray[8,0].AssignAsNewEmpty();
                  gamePieceArray[8,1].AssignAsNewEmpty();
                  gamePieceArray[8,2].AssignAsNewEmpty();
                  gamePieceArray[8,3].AssignAsNewEmpty();
                  gamePieceArray[8,4].AssignAsNewEmpty();
                  gamePieceArray[8,5].AssignAsNewEmpty();
                  gamePieceArray[8,6].AssignAsNewEmpty();
                  gamePieceArray[8,7].AssignAsNewEmpty();
                  gamePieceArray[8,8].AssignAsNewEmpty();
                  gamePieceArray[8,9].AssignAsNewEmpty();
                  gamePieceArray[9,0].AssignAsNewEmpty();
                  gamePieceArray[9,1].AssignAsNewEmpty();
                  gamePieceArray[9,2].AssignAsNewEmpty();
                  gamePieceArray[9,3].AssignAsNewEmpty();
                  gamePieceArray[9,4].AssignAsNewEmpty();
                  gamePieceArray[9,5].AssignAsNewEmpty();
                  gamePieceArray[9,6].AssignAsNewEmpty();
                  gamePieceArray[9,7].AssignAsNewEmpty();
                  gamePieceArray[9,8].AssignAsNewEmpty();
                  gamePieceArray[9,9].AssignAsNewEmpty();
            }
      }
}




Here is my code for the Game Piece class:


using System;

namespace robotz
{
      /// <summary>
      /// Summary description for GamePiece.
      /// </summary>
      public class GamePiece
      {
            public const int MasterRobot = 1;
            public const int Medic = 2;
            public const int Engineer = 4;
            public const int Destroyer = 8;
            public const int Wall = 256;
            public const int Empty = 512;

            public string owner;
            public int health;
            public int attack_strength;
            public string image_filename;

            //1 = MR
            //2 = MED
            //4 = ENG
            //8 = DEST
            //256 = WALL
            //512 = EMPTY

            public int piece_type;

            public GamePiece()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }

            public string DisplayImage()
            {
                  return image_filename;
            }

            public void AssignAsNewMasterRobot(string temp_owner)
            {
                  owner = temp_owner;
                  piece_type = MasterRobot;
                  attack_strength = 0;
                  health = 200;
                  image_filename = "mr.jpg";
            }

            public void AssignAsNewMedic(string temp_owner)
            {
                  owner = temp_owner;
                  piece_type = Medic;
                  attack_strength = 20;
                  health = 100;
                  image_filename = "med.jpg";
            }

            public void AssignAsNewEngineer(string temp_owner)
            {
                  owner = temp_owner;
                  piece_type = Engineer;
                  attack_strength = 10;
                  health = 100;
                  image_filename = "eng.jpg";
            }

            public void AssignAsNewDestroyer(string temp_owner)
            {
                  owner = temp_owner;
                  piece_type = Engineer;
                  attack_strength = 20;
                  health = 100;
                  image_filename = "dest.jpg";
            }

            public void AssignAsNewWall()
            {                  
                  piece_type = Wall;            
                  image_filename = "wall.jpg";
            }

            public void AssignAsNewEmpty()
            {                  
                  piece_type = Empty;            
                  image_filename = "blank.jpg";
            }
      }
}
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom KnowltonWeb developerAuthor Commented:
UPDATE:


This seemed to do the trick:


public void InitPieces()
            {
                  for(int i=0;i<10;i++)
                  {
                        for(int j=0;j<10;j++)
                        {
                              gamePieceArray[i,j] = new GamePiece();
                        }
                  }
                  
            }

            public void InitNewGame(string player1, string player2)
            {
                  InitPieces();






My question now is......is this the CORRECT way to initialize an array of objects?
0
dsaboCommented:
YES.

I think thats the only way to initialize the objects in the matrix.
0
cambo1982Commented:
Hey Knowlton,

Yes your correct,

When you create

public GamePiece[,] gamePieceArray = new GamePiece[10,10];
public int x;

This creates a 2D Array Space in memory, of empty objects, same way you create an un-initialized integer.

Once you've created your array of objects you then need to initialize each individual object in the array. The for loop structure you've used is correct but as a general tip, i'd advise not hardcoding your array sizes into the loop. Its just good practice for extensibility. If you ever needed to change your board size, you'd have to go thru every line of code looking for the board width/height

for(int i = 0; i <= gamePieceArray.getUpperBound(0); i++)
    for(int j = 0;j <= gamePieceArray.getUpperBound(1); j++)
         gamePieceArray[i,j] = new GamePiece();

//use <= because upper bound returns last index on that dimension
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.