How to Resolve Java.lang.ExceptionInInitializerError In Java?

03 Mar, 2022
An unexpected, unwanted event that disturbed the normal flow of a program is called an Exception.
There are mainly two types of exception in Java:
1. Checked Exception
2. Unchecked Exception
ExceptionInInitializerError is the child class of the Error class and hence it is an unchecked exception. This exception is rise automatically by JVM when JVM attempts to load a new class as, during class loading, all static variables and static initializer block are being evaluated. This exception also acts as a signal that tells us that an unexpected exception has occurred in a static initializer block or in the assignment of value to the static variable.
There are basically two cases when ExceptionInInitializerError can occur in a Java Program:
1. ExceptionInInitializerError While Assigning Value To The Static Variable
In the below example we assign a static variable to 20/0 where 20/0 gives an undefined arithmetic behavior and hence there occurs an exception in the static variable assignment and ultimately we will get ExceptionInInitializerError.
Java
// Java Program for showing the ExceptionInInitializerError// While Assigning Value To The Static Variableclass GFG {    // assignment of static variable    static int x = 20 / 0;    public static void main(String[] args)    {        // printing the value of x        System.out.println("The value of x is " + x);    }}
  
 
2. ExceptionInInitializerError While Assigning Null Value Inside A Static Block
 
In the below example we have declared a static block inside which we create a string s and assign a null value to it, and then we are printing the length of string, so we will get NullPointerException because we were trying to print the length of a string that has its value as null and as we see that this exception occurs inside the static block, so we will get ExceptionInInitializerError.
 
Java
// Java Program for showing the ExceptionInInitializerError// While Assigning Null Value Inside A Static Blockclass GFG {    // declaring a static initializer  block    static    {        // creating a string and assigning a null value to        // it        String s = null;        // printing the length of string but as the string        // is null so an exception occur in the static block        System.out.println(s.length());    }    public static void main(String[] args)    {        System.out.println("GeeksForGeeks Is Best");    }}
  
 
How to Resolve Java.lang.ExceptionInInitializerError ?
 
We can resolve the java.lang.ExceptionInInitializerError by ensuring that static initializer block of classes does not throw any Runtime Exception.
We can resolve also resolve this exception by ensuring that the initializing static variable of classes also doesn’t throw any Runtime Exception.
 
adnanirshad158
avtarkumar719
Java-Exceptions
Picked
Technical Scripter 2020
How To
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Set Git Username and Password in GitBash?
How to Install Jupyter Notebook on MacOS?
How to Install and Use NVM on Windows?
How to Install Python Packages for AWS Lambda Layers?
How to Add External JAR File to an IntelliJ IDEA Project?
Arrays in Java
Split() String method in Java with examples
Arrays.sort() in Java with examples
Object Oriented Programming (OOPs) Concept in Java
Reverse a string in Java