0

I'm new in C# and have a Issue with my coding. I got an error during runtime. It's seems object array isn't assigned. Any help/hints are welcome. I have a class "Building" with fix rooms. No change or additional rooms are foreseens. The rooms are discribed by a lots of attributes. In my "Home" class I want to access all in rooms via object array and also specific rooms. Thanks in advance:

Class Home
{
private void StartProgramm()
        {

            //get all Objects of class "Building"
            Building[] building = Building.Buildings;

            //System.NullReferenceException during runtime

            set_label_header(building[0].Name);

            //Access to room1
            Building room1 = Building.room1;

            set_label_header(room1.Name);

            //Get room2
            Building room2 = Building.GetBuilding("room2");

            set_label_header(room2?.Name);



         }
}


    class Building
    {

        public string Name { get; set; }
        public ArrayList tool_groupList = new ArrayList();
        public ArrayList pos_List = new ArrayList();
        public ArrayList inv_list = new ArrayList();


        public static Building room1;
        public static Building room2;
        public static Building[] Buildings;


    public Building()
            {
            //create room 1 and add some attributes
            room1 = new Building { Name = "room1" };
            room1.tool_groupList.Add(1);
            room1.tool_groupList.Add(2);
            room1.pos_List.Add(14);
            room1.pos_List.Add(15);
            room1.pos_List.Add(16);
            room1.pos_List.Add(17);
            Iventory[] inventories = Inventory.get_inventories();
            room1.areaList.AddRange(inventories);



            //create room 2 and add some attributes
            room2 = new Building { Name = "room2" };
            room2.tool_groupList.Add(2);
            room2.pos_List.Add(1);
            room2.pos_List.Add(2);
            room2.pos_List.Add(9);
            room2.pos_List.Add(10);
            room2.pos_List.Add(13);
            room2.pos_List.Add(14);
            room2.pos_List.Add(15);
            room2.pos_List.Add(16);
            Iventory[] inventories = Inventory.get_inventories();
            room2.areaList.AddRange(inventories);


            //Array with all rooms
            Buildings = new Building[] { room1, room2 };


        }

        //get single room by name

        public static Building GetBuilding(string name)
        {
            foreach (Building Building in Buildings)
            {
               if( Building.Name != null && Building.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                {
                    return Building;
                }

            }
            return null;
        }





    }
Jessica
  • 19
  • 2

1 Answers1

2

You are trying to access the static field Building.Buildings before the constructor of the Class Building is called. After you create an instance of Building the static field would be assigned. You probably want to use a static constructor like in the following example:

static Building()
{
    ...
    Buildings = new Building[] { room1, room2 };
}
Tiago
  • 68
  • 6