Reputation: 8830
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 not ArrayIndexOutOfBoundException
.
Upvotes: 8
Views: 117840
Reputation: 1642
ExceptionInInitializerError is an Unchecked Exception.
While executing, the static block, static variable initialization, if any exception comes then it is ExceptionInInitializerError.
example:
class Test{
static int x = 10/0;
}
output:
Runtime Exception: ExceptionInInitializerError caused by java.lang.ArithmeticExcpetion.
example:
class Test{
static{
String s = null;
System.out.println(s.length());
}
}
output:
Runtime Exception: ExceptionInInitializerError caused by java.lang.NullPointerException.
Upvotes: 0
Reputation: 122026
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:
You hit by the bolder point, since the array is null
.
Upvotes: 12
Reputation: 6557
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.
Upvotes: 3
Reputation: 6428
NullPointerException: This exception is thrown when you try to access the properties of an uninitialized object
ArrayIndexOutOfBoundsException: This exception is thrown when the array is initialized with an object but you try to access the array with invalid index.
In your case since you haven't initialized your object you are getting NullPointerException. You have created a person named "x" but have not associated any human being(array object).
If you change line 2 to,
static int x[] = new int[];
then you will get ArrayIndexOutOfBoundsException instead of NullPointerException.
Upvotes: 0
Reputation:
It is simple. Here x is null
and you are trying to store a value in uninitialized array
.Hence NullPointerException
Upvotes: 1
Reputation: 3197
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.
Upvotes: 2
Reputation: 8598
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
}
Upvotes: 1
Reputation: 4588
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
Upvotes: 1