java.lang.exceptionininitializererror – How to handle Exception Initializer Error
In this tutorial we will discuss about Java’s ExceptionInInitializerError
and how to deal with it. The ExceptionInInitializerError
is a sub-class of the LinkageError
class and denotes that an unexpected exception has occurred in a static initializer or the initializer for a static variable. As of Java release 1.4, this error conforms to the general purpose exception-chaining mechanism.
The ExceptionInInitializerError
is thrown when the JVM attempts to load a new class. During the class loading procedure, all static variables and static initializers are being evaluated. A static initializer is a block enclosed within curly braces without having any name and return type, except having the keyword static
.
A sample example of a static initializer is shown below:
import java.util.UUID; class Example { private static String ID = null; static { ID = UUID.randomUUID().toString(); } }
The static initializer is evaluated only once during the class loading procedure. Thus, a thrown exception in the evaluation of a static variable or initializer is wrapped into an ExceptionInInitializerError
, in order for the JVM to indicate that the class could not be initialized and loaded.
A sample example that throws the aforementioned error is the following:
Example.java:
public class Example { private static String message = null; private static String subMessage = null; public Example(String message) { Example.message = message; } static { /* Store the first 10 characters of the input message. */ subMessage = message.substring(0, 10); } public String getSubMessage() { return subMessage; } public static void main(String[] args) { Example exampleClass = new Example("Test"); System.out.println(exampleClass.getSubMessage()); } }
If we execute the above code snippet, we will receive the following error:
Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException at main.java.Example.<clinit>(Example.java:13)
We can use the following methods, in order to retrieve more information about the underlying exception:
getException()
: Returns the exception that occurred during a static initialization that caused this error to be created. However, after the exception has conformed to the general-purpose exception chaining, the preferred method to use isgetCause
.getCause()
:Returns the exception that caused this error to be thrown.
How to deal with the ExceptionInInitializerError
The ExceptionInInitializerError
is used as a wrapper to indicate that an exception arises in the static initializer block or the evaluation of a static variable’s value. Thus, we have to ensure that the original exception is fixed, in order for the JVM to be able to load our class successfully.
Final comments on the Error
It is very important to mention that you can throw unchecked
/runtime
exceptions from the block of a static initializer. However, you cannot allow a checked exception to propagate out of a static block, because is not possible to handle these exceptions in your source.
This was a tutorial about Java’s ExceptionInInitializerError
.
where is the solution for this error: ExceptionInInitializerError ? If it is not possible to handle these exceptions in your source means then how to handle in base class.
System.exit(1);
Like here it is said, ExceptionInInitializerError is a wrapper for the root cause. Once you know the actual cause, you should be able to fix it and since this error is only thrown in exceptions in static block(which gets initialised once for every class), once you have fixed the issue in static block, you should be good.
Where did you mention about handling it?