File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ExceptionInInitializerError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ExceptionInInitializerError" Watch "ExceptionInInitializerError" New topic
Author

ExceptionInInitializerError

Ash Gill
Ranch Hand

Joined: Jun 30, 2011
Posts: 71
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.

many thanks and regards
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi,

Following is the sample code that throws the java.lang.ExceptionInInitializerError error.



Thanks..


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Ash Gill
Ranch Hand

Joined: Jun 30, 2011
Posts: 71
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.
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi Ash,

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.

Hope this helps
Thanks
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Sorry for waking a zombie !!

Vijay Tidake wrote: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.

When I tried to do the above to load a class I got only a java.lang.NoClassDefFoundError and not java.lang.ExceptionInInitializerError.

This is the class I tried.



Error - Note that javac and one of the java commands have the log4j jar in its classpath and the the last java command don't.



Just made a sample program based on the comments in the API - ExceptionInInitializerError

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ExceptionInInitializerError
 
Similar Threads
static and final
Inializing a Final Variable
Singleton Design Pattern
need for constructors in this case
Doubt with static initializers