获取错误“线程“主”中的异常java.lang.ExceptionInInitializerError”

时间:2019-05-27 05:46:15

标签: java

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

请找到堆栈跟踪

type Idea @model
 {
  id: ID!
  title: String!
  userName: String
  category: String!
  status:String
  OtherCategory: String
  description: String
  subCategory: String   
  createdAt: AWSDateTime
  updatedAt: AWSDateTime
  comments: [Comment] @connection(name: "IdeaComments")
}

1 个答案:

答案 0 :(得分:2)

应该是:

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变量null仍然是put

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

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