wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why its throwing ExceptionInInitializerError? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why its throwing ExceptionInInitializerError?" Watch "Why its throwing ExceptionInInitializerError?" New topic
Author

Why its throwing ExceptionInInitializerError?

Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186




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



What is the problem in first?

I acknowledge your replies. Thanks.


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

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

Joined: Mar 26, 2012
Posts: 186

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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Mar 26, 2012
Posts: 186

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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Apr 04, 2012
Posts: 924
    
    1

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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Apr 04, 2012
Posts: 924
    
    1

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
Bartender

Joined: May 31, 2011
Posts: 1776
No problem... we will wait for other ranchers to clear your confusion.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Feb 09, 2012
Posts: 583

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.


Tell the difficulties that i am difficult.
 
 
subject: Why its throwing ExceptionInInitializerError?
 
Similar Threads
Does static methods and variables get inherited ?
what does this mean here?
Is there any way to convert an int to a float ?
Doubt in static...
error in following code