Java. lang. ExceptionInInitializerError exception,

Source: Internet
Author: User

Java. lang. ExceptionInInitializerError exception,

During the development process today, Baidu encountered an exception in java. lang. ExceptionInInitializerError.

If an unexpected exception occurs in the static initialization program, the ExceptionInInitializerError is thrown, indicating that an exception occurs during the calculation of the initial value of the static variable or the initial value of the static variable.

 

To understand this exception, starting from the initialization process of static variables in the Java class, the initialization sequence of static variables in the Java class is consistent with the declaration sequence of static variables. The sample code is as follows:

1 package com. lang. ininitialException; 2 3 import java. util. arrayList; 4 import java. util. list; 5 6 public class StaticParams {7 8 private static int NUM_A = getA (); 9 private static int NUM_ B = getB (); 10 private static List <String> LIST_A = getListA (); 11 12 13 private StaticParams () {14 System. out. println ("Initial constructor"); 15} 16 17 public static StaticParams getInstance () {18 return new StaticParams (); 19} 20 21 private static int getA () {22 System. out. println ("initialize A"); 23 return 5; 24} 25 26 private static int getB () {27 System. out. println ("initialize B"); 28 return 10; 29} 30 31 private static List <String> getListA () {32 System. out. println ("initialization List"); 33 return new ArrayList <String> (); 34} 35 36 public static void main (String args []) {37 StaticParams. getInstance (); 38} 39}

Running result:

Initialize
Initialize B
Initialize List
Initial Constructor

If the positions of NUM_A and NUM_ B are exchanged, the result is:

Initialize B
Initialize
Initialize List
Initial Constructor

That is, the static variable is used before the initialization of a static variable is executed.

The program to reproduce the ExceptionInInitializerError exception is as follows:

1 package com. lang. ininitialException; 2 3 import java. util. arrayList; 4 import java. util. list; 5 6 public class StaticParamsSingle {7 8 private static StaticParamsSingle sps = buildStaticParams (); 9 // private static StaticParamsSingle sps = new StaticParamsSingle (); 10 private static int NUM_A = getA (); 11 private static int NUM_ B = getB (); 12 private static List <String> LIST_A = getListA (); 13 14 private StaticParamsSingle () {15 System. out. println ("initialization constructor"); 16} 17 18 private static StaticParamsSingle buildStaticParams () {19 if (sps = null) {20 sps = new StaticParamsSingle (); 21} 22 23 int result = NUM_A + NUM_ B; 24 System. out. println ("result is:" + result); 25 LIST_A.add ("haha"); 26 return sps; 27} 28 29 public static StaticParamsSingle getInstance () {30 return sps; 31} 32 33 private static int getA () {34 System. out. println ("initialize A"); 35 return 5; 36} 37 38 private static int getB () {39 System. out. println ("initialize B"); 40 return 10; 41} 42 43 private static List <String> getListA () {44 System. out. println ("initialization List"); 45 return new ArrayList <String> (); 46} 47 48 public static void main (String args []) {49 StaticParamsSingle. getInstance (); 50 51} 52}

Other variables used in sps variable initialization, but other static variables have not yet been initialized, so exceptions will occur during sps initialization:

Initialization Constructor
Result is: 0
Exception in thread "main" java. lang. ExceptionInInitializerError
Caused by: java. lang. NullPointerException
At com. lang. ininitialException. StaticParamsSingle. buildStaticParams (StaticParamsSingle. java: 25)
At com. lang. ininitialException. StaticParamsSingle. <clinit> (StaticParamsSingle. java: 8)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.