• Post Reply Bookmark Topic Watch Topic
  • New Topic

ExceptionIninitializerError  RSS feed

 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ExceptionIninitializerError is generally thrown by static block or static variable can anyone please try to make me understand how to know whether it is ExceptionInInitializererror or someother in this two codes.because the result in code1 is showing ExceptionInInitializererror and in code2 NullPointer exception....


code 1 :
public class Demo {
static string name=null;
static int namelength = name.length(); -----> throwing ExceptionInInitializererror
}


code 2:
class Demo1{
static String [] old;
system.out.println(old[1]); -------->NullPointer exception


please explain me with reasons...
 
Henry Wong
author
Sheriff
Posts: 23034
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This...

meghana chintanippu wrote:
code 2:


please explain me with reasons...


Does *not* compile.

Perhaps, you can elaborate a bit.

Henry
 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my question is why is the code2 not showing ExceptionInInitializerError but showing NullPointer exception eventhough String []old is static and is not intialized so it will be assingened a value null by default . so as it is static why is it not EceptionInInitializerError ??
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meghana chintanippu wrote:my question is why is the code2 not showing ExceptionInInitializerError but showing NullPointer exception eventhough String []old is static and is not intialized so it will be assingened a value null by default . so as it is static why is it not EceptionInInitializerError ??

Post the exact code that you compiled and ran. Also, check your stacktrace carefully. It might say ExceptionInInitializerError caused by NullPointerException.
 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!