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

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

獲取錯(cuò)誤“線程“主”java.lang.ExceptionInInitializerError ”

獲取錯(cuò)誤“線程“主”java.lang.ExceptionInInitializerError ”

holdtom 2022-12-15 15:19:27
我正在嘗試用 Enum Month 初始化 HashMap。但是,出現(xiàn)如下錯(cuò)誤:“線程“主”java.lang.ExceptionInInitializerError 中的異?!?。請(qǐng)讓我知道為什么會(huì)出現(xiàn)此錯(cuò)誤。我在靜態(tài)主類方法和行中得到這個(gè)ChatGroups.initializeMap();package static_initialization;import java.lang.reflect.Type;import java.util.HashMap;import java.util.Map;public class ChatGroups {    public static final Map<String, Month> months = new HashMap<String, Month>() {{        months.put("Jan", Month.January);        months.put("Feb", Month.February);        months.put("Mar", Month.March);        months.put("Apr", Month.April);        months.put("Jun", Month.June);        months.put("Jul", Month.July);        months.put("Aug", Month.August);        months.put("Sep", Month.September);        months.put("Oct", Month.October);        months.put("Nov", Month.November);        months.put("Dec", Month.December);    }};    public static void initializeMap() {        System.out.println();    }}package static_initialization;public enum Month {    January(0),    February(1),    March(2),    April(3),    May(4),    June(5),    July(6),    August(7),    September(8),    October(9),    November(10),    December(11);    private int month;    Month(int i) {        month = i;    }}package static_initialization;public class Main {    public static void main(String[] args) {        initializeMaps();    }    public static void initializeMaps() {        try {            ChatGroups.initializeMap();        } catch (Exception ex) {            System.out.println("Neelabh exception = " + ex);        }    }}請(qǐng)找到堆棧跟蹤Exception in thread "main" java.lang.ExceptionInInitializerError    at static_initialization.Main.initializeMaps(Main.java:10)    at static_initialization.Main.main(Main.java:5)Caused by: java.lang.NullPointerException    at static_initialization.ChatGroups$1.<init>(ChatGroups.java:9)    at static_initialization.ChatGroups.<clinit>(ChatGroups.java:8)
查看完整描述

1 回答

?
繁星coding

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

它應(yīng)該是:


public static final Map<String, Month> months = new HashMap<String, Month>() {{

    put("Jan", Month.January);

    put("Feb", Month.February);

    put("Mar", Month.March);

    put("Apr", Month.April);

    put("Jun", Month.June);

    put("Jul", Month.July);

    put("Aug", Month.August);

    put("Sep", Month.September);

    put("Oct", Month.October);

    put("Nov", Month.November);

    put("Dec", Month.December);

}};

months.put()導(dǎo)致NullPointerException,因?yàn)閳?zhí)行語(yǔ)句時(shí)static變量months仍然存在。nullput


作為替代方案,避免創(chuàng)建匿名類實(shí)例,而是編寫:


public static final Map<String, Month> months = new HashMap<String, Month>();

static {

    months.put("Jan", Month.January);

    months.put("Feb", Month.February);

    months.put("Mar", Month.March);

    months.put("Apr", Month.April);

    months.put("Jun", Month.June);

    months.put("Jul", Month.July);

    months.put("Aug", Month.August);

    months.put("Sep", Month.September);

    months.put("Oct", Month.October);

    months.put("Nov", Month.November);

    months.put("Dec", Month.December);

}


查看完整回答
反對(duì) 回復(fù) 2022-12-15
  • 1 回答
  • 0 關(guān)注
  • 83 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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