java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException
Solution 1
Why it is throwing NullPointerException and not ArrayIndexOutOfBoundException.
Because you did not initialize the array.
Initialize array
static int x[] = new int[10];
Reason for NullPointerException:
Thrown when an application attempts to use null in a case where an object is required. These include:
- Calling the instance method of a null object.
- Accessing or modifying the field of a null object.
- Taking the length of null as if it were an array.
- Accessing or modifying the slots of null as if it were an array.
- Throwing null as if it were a Throwable value.
You hit by the bolder point, since the array is null
.
Solution 2
It's throwing NullPointerException because x is null
.
x[] is declared, but not initialized.
Before initialization, objects have null value, and primitives have default values (e.g. 0, false etc)
So you should initialize as shown below:
static int x[] = new int[20];
//at the time of declaration of x
or
static int x[];
x = new int[20];//after declaring x[] and before using x[] in your code
ArrayIndexOutOfBoundException will occur if array is initialized and accessed with an illegal index.
e.g :
x contains 20 elements, so index numbers 0 to 19 are valid, if we access with anyindex < 0
or
index > 19
, ArrayIndexOutOfBoundException will be thrown.
Solution 3
The NullPointerException
is thrown out in the static block
, where you are trying to assign a value 1 to the first element of array (x[0] = 1). Be aware, the int[] array named x is still not intilized.
public class Test {
static int x[];
static {
x[0] = 1;// Here is the place where NullPointException is thrown.
}
public static void main(String... args) {
}
}
There are 2 ways for you to fix it.
1 Use static int x[] = new int[5];
instead of static int x[] ;
2
Change
static {
x[0] = 1;
}
To
static {
x= new int[5];
x[0] = 1;
}
Remember: Initialize the array before you use it.
Solution 4
You didn't initialize your x
array. There is a difference between declaration and initialization of variables. When you write int x[];
you just declare a variable which, as an instance field, is initialized with a default value of null
. To actually create an array you must write int x[] = new int[10];
or the size you need.
The reason for getting a NullPointerException
instead of ArrayIndexOutOfBounds
is that the latter is thrown when you do have an array and try to address a position out of its bounds, but in your case you don't have an array at all and try to put something into a non-exsting array. That's why an NPE
Solution 5
static int x[];
static {
x[0] = 1;
}
Results in NullPointerException, because your x array in not initialised (is null)
ArrayIndexOutOfBoundException would happen if you accessed an index that is out of bounds:
static int x[] = new int[10];
static {
x[20] = 1; //<-----accessing index 20
}
Prashant Shilimkar
Updated on October 03, 2020Comments
-
Prashant Shilimkar over 3 years
This is from OCJP example. I have written a following code
public class Test { static int x[]; static { x[0] = 1; } public static void main(String... args) { } }
Output: java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException at
x[0] = 1;
Why it is throwing
NullPointerException
and notArrayIndexOutOfBoundException
.