Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why its throwing ExceptionInInitializerError?

 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



But I see that this works well in storing object in static variable:



What is the problem in first?

I acknowledge your replies. Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 21409
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajdeep Biswas wrote:

What is the problem in first?

I acknowledge your replies. Thanks.



Simply put, it is a null pointer exception.

Your static initialization of the obj variable, creates an instance. And your instance initialization uses the instance that you created -- and unfortunately, during the creating of the instance for the static variable, that object isn't available yet.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21409
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Side Note: I have to admit that this is one of the handful of mistakes that I seem to have made more than once -- meaning I don't seem to learn from the mistake...

Static variables are initialized when the class is loaded. Instance variables are initialized when an instance is created. And every instinct tells you that the first always completes before the second -- and that during instantiation, you can assume that the static variables are all initialized. Of course, this is not true, as shown in this example.

Henry



 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sheriff. Got it.

Henry Wong wrote:Side Note: I have to admit that this is one of the handful of mistakes that I seem to have made more than once -- meaning I don't seem to learn from the mistake...

Static variables are initialized when the class is loaded. Instance variables are initialized when an instance is created. And every instinct tells you that the first always completes before the second -- and that during instantiation, you can assume that the static variables are all initialized. Of course, this is not true, as shown in this example.

Henry





But why above is not the case here?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as Henry already said - Static is executed while the class gets load. The static call creates an object. It's obvious that since the initialization block is called for the object creation and the object is not yet created (the call itself is made for this obj object creation), you get the NullPointerException while accessing the object's variables. Note that only static blocks during initialization will lead to ExceptionInInitializerError.

Not sure what is still confusing you.
 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost at the brink of clearance of doubt. Is it going as follows?

When I am writing this


then
1. instance block is executed [and causes ExceptionInInitializerError (just a special case of NullPointerException)],
2. constructor is called,
3. the reference is stored in "obj" variable.
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajdeep Biswas wrote:and causes ExceptionInInitializerError (just a special case of NullPointerException)],

ExceptionInInitializerError is completely different from NullPointerException. One is an Error and other is an Exception.
When an Exception occurs in the static initializer, it is wrapped in an ExceptionInInitializerError and thrown. Here since a NullPointerException has occurred while initializing the static variable, you get the error.

I have added print statements and also commented a code to cause ArithmeticException. Uncomment the line, and see ExceptionInInitializerError is thrown not only for NullPointerException.



You might also check if this thread helps.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost at the brink of clearance of doubt. Is it going as follows?

When I am writing this
view plaincopy to clipboardprint?

public static ObjCreateDemo obj = new ObjCreateDemo();



then
1. instance block is executed [and causes ExceptionInInitializerError (just a special case of NullPointerException)],
2. constructor is called,
3. the reference is stored in "obj" variable.


Frankly speaking im not able to get the answers. and i do not know whats the reason for the exception. but regarding your order i would like to tell you when we do new ObjCreateDemo(), the compiler supplied no-arg constructor gets called , which calls super() i.e. object class constructor . after the Object class constructor executes the control return back to ObjCreateDemo constructor. After that instance initialization blocks are called , and after that rest of the statements in constructor gets executed. Please henry would like you to clear our doubts and elaborate your answer ? And Raj if you know the answer now, please i would like an explanation from your side as well ?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:but regarding your order i would like to tell you when we do new ObjCreateDemo(), the compiler supplied no-arg constructor gets called , which calls super() i.e. object class constructor . after the Object class constructor executes the control return back to ObjCreateDemo constructor. After that instance initialization blocks are called , and after that rest of the statements in constructor gets executed

Gurpeet - You got the flow correct
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John but i did not got the solution for the original question. i'm sorry but i didn't got your answers properly. can you please provide another explanation ?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem... we will wait for other ranchers to clear your confusion.
 
Henry Wong
author
Marshal
Pie
Posts: 21409
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:
gurpeet singh wrote:but regarding your order i would like to tell you when we do new ObjCreateDemo(), the compiler supplied no-arg constructor gets called , which calls super() i.e. object class constructor . after the Object class constructor executes the control return back to ObjCreateDemo constructor. After that instance initialization blocks are called , and after that rest of the statements in constructor gets executed

Gurpeet - You got the flow correct



While all of this is correct, it is also completely moot. You are overthinking the problem.

Go back to this line of code...



This is a static variable, and its initialization is done right after the class is loaded. And execution is simple. Instantiate an ObjCreateDemo object, and assign it to the obj variable. It must instantiate the object first, and then assign it to the obj variable. Until the varlable has been instantiated, and then assigned, the "obj" variable has a default value of null..... this means that during the instantiation process of the first ObCreateDemo object, the valus of the "obj" instance is null.


Now, unfortunately, during the instantiation of the ObjCreateDemo object, it uses the obj variable -- a variable that hasn't been assigned until after the first object has been completed.

Henry
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys. i have understood the problem. put bits and bytes from all your answers and i was good to go.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:
Almost at the brink of clearance of doubt. Is it going as follows?

When I am writing this
view plaincopy to clipboardprint?

public static ObjCreateDemo obj = new ObjCreateDemo();



then
1. instance block is executed [and causes ExceptionInInitializerError (just a special case of NullPointerException)],
2. constructor is called,
3. the reference is stored in "obj" variable.


Frankly speaking im not able to get the answers. and i do not know whats the reason for the exception. but regarding your order i would like to tell you when we do new ObjCreateDemo(), the compiler supplied no-arg constructor gets called , which calls super() i.e. object class constructor . after the Object class constructor executes the control return back to ObjCreateDemo constructor. After that instance initialization blocks are called , and after that rest of the statements in constructor gets executed. Please henry would like you to clear our doubts and elaborate your answer ? And Raj if you know the answer now, please i would like an explanation from your side as well ?


Initialization block always execute first before any constructor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic