ExceptionInInitializerError ?

What is the error all about ? Why is this coming with this program ?

I know there is some mistake in static block.

CertkillerTe2.java

public class CertkillerTe2 {

static int[] a; // static variable

static {

a[0] = 2;

} // static block

public static void main(String[] args) { }

}

Output :

D:\>javac CertkillerTe2.java

D:\>java CertkillerTe2

Exception in thread "main" java.lang.ExceptionInInitializerError

Caused by: java.lang.NullPointerException

at CertkillerTe2.<clinit>(CertkillerTe2.java:4)

[606 byte] By [Tatona] at [2007-10-3 3:00:20]
# 1
> Caused by: java.lang.NullPointerExceptionSo where do you instantiate that array?
CeciNEstPasUnProgrammeura at 2007-7-14 20:49:55 > top of Java-index,Java Essentials,New To Java...
# 2

You need to iniialse the array. The following works OK

public class CertkillerTe2 {

static int[] a = new int[10]; // static variable

static {

a[0] = 2;}

//} // static block

public static void main(String[] args) { }

}

Fo primitive arrays like this the following quote is informative. There are other structures that are dynamic and usually more useful than a int[] or other primitive array.

"Unlike some programming languages, in Java, arrays have to be specifically created (or declared) and are of a definite size. It is not possible to change the size of an array once you have declared its size, you can, of course, copy the contents to another, larger, array."

Message was edited by:

nerak99

nerak99a at 2007-7-14 20:49:55 > top of Java-index,Java Essentials,New To Java...
# 3

> Fo primitive arrays like this the following quote is

> informative. There are other structures that are

> dynamic and usually more useful than a int[] or other

> primitive array.

Note that those structures can not handle primitives (even though it may look that way through autoboxing in 1,5,0).

CeciNEstPasUnProgrammeura at 2007-7-14 20:49:55 > top of Java-index,Java Essentials,New To Java...
# 4
An excellent point monsieur IMO
nerak99a at 2007-7-14 20:49:55 > top of Java-index,Java Essentials,New To Java...
# 5
static int[] a = new int[size]; Ok, thanks
Tatona at 2007-7-14 20:49:55 > top of Java-index,Java Essentials,New To Java...