Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Object reference not set to an instance of an object.

Posted on 2004-11-12
3
Medium Priority
?
228 Views
Last Modified: 2010-08-05
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";
            }
      }
}
0
Comment
Question by:Tom Knowlton
3 Comments
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12571586
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
 
LVL 2

Assisted Solution

by:dsabo
dsabo earned 800 total points
ID: 12571711
YES.

I think thats the only way to initialize the objects in the matrix.
0
 
LVL 1

Accepted Solution

by:
cambo1982 earned 1200 total points
ID: 12578274
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question