gpt4 book ai didi

java - 出现错误 "Exception in thread "main“java.lang.ExceptionInInitializerError”

转载 作者:行者123 更新时间:2023-11-30 01:50:18 25 4
gpt4 key购买 nike

我正在尝试使用 Enum Month 初始化 HashMap。但是,出现如下错误:“线程“main”java.lang.ExceptionInInitializerError 中出现异常”。请让我知道为什么我会收到此错误。我在静态主类方法和 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);
}
}
}

请查找堆栈跟踪

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)

最佳答案

应该是:

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() 导致 NullPointerException,因为 static 变量 months 仍然是 执行 put 语句时为 null

作为替代方案,避免创建匿名类实例,而是编写:

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);
}

关于java - 出现错误 "Exception in thread "main“java.lang.ExceptionInInitializerError”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56320275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com