Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ExceptionInInitializerError

 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
Hibernate Java Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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



Thanks..
 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic