java. Solution to lang.exceptionininitializererror exception

Today, in the process of development, I encountered Java Lang. exceptionininitializererror exception. Baidu checked it and took a note

A signal of an unexpected exception in the static initializer. Throwing exceptionininitializererror indicates that an exception occurred during the calculation of the static initial value or the initial value of the static variable.

To understand this exception from @ r_ 419_ The static variable initialization process in 1713 @ starts at @ R_ 419_ The initialization order of static variables in 1713 @ is consistent with the declaration order of static variables. The sample program is:

Operation results:

Initialization a initialization B initialization list initial construction method

If you swap num_ A and num_ B position, then the result is:

Initialization B initialization a initialization list initial construction method

That is, a static variable is used before its initialization is executed.

The procedure for reproducing the exceptionininitializererror exception is as follows:

Other variables used in SPS variable initialization, but other static variables have not been initialized, so an exception will be generated during SPS initialization:

Initializing 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)

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>