How to convert 10 x 10 2D array over to a jagged array?

///////////////  OLD 2D Array //////////////////
//      public GameObject[,] world_objects = new GameObject[MAX_ROWS,MAX_COLS];



/////////////  NEW JAGGED ARRAY //////////////////
            public GameObject[][] world_objects = new GameObject[MAX_ROWS][];

            public World()
            {
                  //InitImagesHash();
                  InitImages();

                  for(int row = 0;row < MAX_ROWS;row++)
                  {
                        for(int col = 0;col < MAX_COLS;col++)
                        {
                              world_objects[row][col] = new MasterRobot();
                        }
                  }




I am getting an error message:

Additional information: Object reference not set to an instance of an object.




How do I properly initialize the jagged array?
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:


Here is my updated code, trying to implement a jagged array:


      [XmlRoot("World")]
      public class World
      {
            [XmlElement("maxrows")]
            public const int MAX_ROWS = 10;
            public const int MAX_COLS = 10;

            [XmlElement("a")]
            public int a = 5;

            [XmlIgnore]
            public Image[] images = new Image[9];

            //[XmlIgnore]
      //      public GameObject[,] world_objects = new GameObject[MAX_ROWS,MAX_COLS];

            [XmlArray("gw")]
            public GameObject[][] world_objects = new GameObject[MAX_ROWS][];

            public World()
            {
                  //InitImagesHash();
                  InitImages();

                  world_objects[0] = new GameObject[10];
                  world_objects[1] = new GameObject[10];
                  world_objects[2] = new GameObject[10];
                  world_objects[3] = new GameObject[10];
                  world_objects[4] = new GameObject[10];
                  world_objects[5] = new GameObject[10];
                  world_objects[6] = new GameObject[10];
                  world_objects[7] = new GameObject[10];
                  world_objects[8] = new GameObject[10];
                  world_objects[9] = new GameObject[10];

                  for(int row = 0;row < MAX_ROWS;row++)
                  {
                        for(int col = 0;col < MAX_COLS;col++)
                        {
                              //world_objects[row][col] = new MasterRobot();
                              world_objects[row][col] = null;
                        }
                  }

                  //world_objects[5][5] = new Wall();
                  //world_objects[2][2] = new Wall();
            }

            public void InitImages()
            {
                  images[0] = Image.FromFile(MasterImageList.FriendlyMasterRobot);
                  images[1] = Image.FromFile(MasterImageList.EnemyMasterRobot);
                  images[2] = Image.FromFile(MasterImageList.FriendlyMedicRobot);
                  images[3] = Image.FromFile(MasterImageList.EnemyMedicRobot);
                  images[4] = Image.FromFile(MasterImageList.FriendlyEngineerRobot);
                  images[5] = Image.FromFile(MasterImageList.EnemyEngineerRobot);
                  images[6] = Image.FromFile(MasterImageList.FriendlyDestroyerRobot);
                  images[7] = Image.FromFile(MasterImageList.EnemyDestroyerRobot);
                  images[8] = Image.FromFile(MasterImageList.Wall);
            }


            public void LoadGame()
            {

            }

            public void DisplayWorld(Graphics gr)
            {
                  for(int row = 0;row < MAX_ROWS;row++)
                  {
                        for(int col = 0;col < MAX_COLS;col++)
                        {
                              //gr.DrawImage(((Image)images[world_objects[row,col].icon]),col * 32, row* 32);                              
                              //gr.DrawImage(images[(int)world_objects[row][col].icon],col * 32, row* 32);                              
                        }
                  }
            }            
      }

      [XmlRoot("GameObject")]
      public class GameObject
      {
            [XmlElement("it")]
            public ITypes icon;

            public GameObject()
            {
            }            
      }

      [XmlRoot("Robot")]
      public class Robot : GameObject
      {
            [XmlElement("own")]
            public string owner;
            [XmlElement("ise")]
            public bool is_enemy;
            
            public Robot()
            {
            }

      }

      [XmlRoot("MasterRobot")]
      public class MasterRobot : Robot
      {
            public MasterRobot()
            {
                  if(!is_enemy)
                  {
                        icon = ITypes.FriendlyMasterRobot;
                  }
                  else
                  {
                        icon = ITypes.EnemyMasterRobot;
                  }
            }
      }








Here is the resulting XML file:

  <?xml version="1.0" ?>
- <World xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <a>5</a>
- <gw>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  </ArrayOfGameObject>
- <ArrayOfGameObject>
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  <GameObject xsi:nil="true" />
  </ArrayOfGameObject>
  </gw>
  </World>
Bob LearnedCommented:
You know Tom, serialization may be nice for somethings, but there is a lot of overhead to XML that I find annoying.  I kind of liked that matrixed method that you had before.  The key for me is to find something that is human-readable and not cumbersome, and not what some consider to be the "best" thing (serialization I mean).  Something that you can tinker with in Notepad.

Bob
Tom KnowltonWeb developerAuthor Commented:
I agree.
Bob LearnedCommented:
For the record, here is an example for jagged arrays:

      int[][] test = new int[5][];

      for (int i = 0; i < 5; i++)
      {
        test[i] = new int[3];

        for (int j = 0; j < 3; j++)
          test[i][j] = i * j;
      }

      int v = test[2][2];

See if it helps.

Bob

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
Tom KnowltonWeb developerAuthor Commented:
I think I may have the XML Serialization working now for my game.
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.