• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

ExceptionInInitializerError in inner classes

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the output is ExceptionInInitializerError caused by java.lang.NullPointerException..
Why the code didn't worked out well?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because static cannot evaluate the initialize I i .
 
Monalisa Das
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic