我是 C# 新手,我的編碼有問(wèn)題。我在運(yùn)行時(shí)遇到錯(cuò)誤。似乎沒(méi)有分配對(duì)象數(shù)組。歡迎任何幫助/提示。我有一個(gè)帶有修理室的“建筑”類。預(yù)計(jì)不會(huì)改變或增加房間。房間有很多屬性。在我的“Home”類中,我想通過(guò)對(duì)象數(shù)組訪問(wèn)所有房間以及特定房間。提前致謝: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); }
在運(yùn)行時(shí)訪問(wèn)對(duì)象數(shù)組 - System.NullReferenceException
達(dá)令說(shuō)
2023-09-16 17:41:55