hello friends, can anyone please explain when does this kind of Error occur.
the book says: Thrown when attempting to initialize a static variable or an initiation block. makes no sense to me as: how can we even try to initialize an initiation block because it doesn't have any name & sattic variables can be initialized, nothing wrong in it.
Following is the sample code that throws the java.lang.ExceptionInInitializerError error.
The important thing is not to stop questioning.Curiosity has its own reason for existing.
Joined: Jun 30, 2011
Hi vijay, thanks a lot. i have some further doubts:
if the line 9 is inside the main() method, then this same exception is thrown there as well. This error is being thrown because the showError points to null. i am still very unclear about this exception.
the API says:
An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.
The main reason behind the ExceptionInInitializerError is that, when the compiler come across the unexpected situation that can not be handled
the compiler will throw the above error.
For example listed below ,when compiler come across the divide by zero situation,it says that the variable is not initialized properly so it throws ExceptionInInitializerError
followed by the cause of exception ie "java.lang.ArithmeticException: / by zero"
Lets take another example ,
If you try to load the class in a static/instance block which is not available in the class path at run time then same error occurs.