第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在運(yùn)行時(shí)訪問對(duì)象數(shù)組 - System.NullReferenceException

在運(yùn)行時(shí)訪問對(duì)象數(shù)組 - System.NullReferenceException

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

1 回答

?
侃侃爾雅

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊

您試圖在調(diào)用Building.Buildings類的構(gòu)造函數(shù)之前訪問靜態(tài)字段Building。創(chuàng)建實(shí)例后,Building靜態(tài)字段將被分配。您可能想使用靜態(tài)構(gòu)造函數(shù),如下例所示:

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


查看完整回答
反對(duì) 回復(fù) 2023-09-16
  • 1 回答
  • 0 關(guān)注
  • 109 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)