Saturday, March 23, 2019

.ExceptionInInitializerError example

public class ExceptionInInitializerError {
    private static String test = null;
    static {
         test = test.substring(0, 5);
    }
    public static void main(String[] args) {
    }
}

Answer : 

When you get an error upfront in the static block itself its called as ExceptionInInitializerError 

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at com.mkyong.ExceptionInInitializerError.<clinit>(ExceptionInInitializerError.java:21)

No comments:

Post a Comment