aspose file tools*
The moose likes Beginning Java and the fly likes ExceptionInInitializerError in inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ExceptionInInitializerError in inner classes" Watch "ExceptionInInitializerError in inner classes" New topic
Author

ExceptionInInitializerError in inner classes

Monalisa Das
Greenhorn

Joined: May 05, 2013
Posts: 11


the output is ExceptionInInitializerError caused by java.lang.NullPointerException..
Why the code didn't worked out well?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

This is what happens:

- When class I is loaded, it is initialized, which means its static member variables are initialized: so, the static member i on line 3 is initialized.
- To initialize i, a new instance of I is created.
- To initialize that new instance of I, an instance of J is created (line 4).
- However, when line 4 is executed, i is still null (because the whole reason line 4 is executed, is because we're initializing the static member i - which isn't done yet).
- When you do i.new J(); when i is null, you get a NullPointerException.
- That gets wrapped in an ExceptionInInitializerError, because that's the kind of error that is thrown when something goes wrong while initializing a class that has just been loaded (remember, this all started with the initialization of class I).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Gaurav. Prasad
Greenhorn

Joined: Jul 03, 2012
Posts: 12
This is because static cannot evaluate the initialize I i .
Monalisa Das
Greenhorn

Joined: May 05, 2013
Posts: 11
Jesper de Jong wrote:This is what happens:

- When class I is loaded, it is initialized, which means its static member variables are initialized: so, the static member i on line 3 is initialized.
- To initialize i, a new instance of I is created.
- To initialize that new instance of I, an instance of J is created (line 4).
- However, when line 4 is executed, i is still null (because the whole reason line 4 is executed, is because we're initializing the static member i - which isn't done yet).
- When you do i.new J(); when i is null, you get a NullPointerException.
- That gets wrapped in an ExceptionInInitializerError, because that's the kind of error that is thrown when something goes wrong while initializing a class that has just been loaded (remember, this all started with the initialization of class I).


Thanks a lot sir.. the reason was simple but was not clicking in my mind...once again thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ExceptionInInitializerError in inner classes
 
Similar Threads
multidimensional array
Why its throwing ExceptionInInitializerError?
how to initialize static final field that the constructor throws some exception
How to handle ExceptionInInitializerError ?