[英]java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException
This is from OCJP example. 这来自OCJP示例。 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 输出: java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException at x[0] = 1;
由以下原因引起:
x[0] = 1;
java.lang.NullPointerException x[0] = 1;
Why it is throwing NullPointerException
and not ArrayIndexOutOfBoundException
. 为什么它抛出
NullPointerException
而不是ArrayIndexOutOfBoundException
。
Why it is throwing NullPointerException and not ArrayIndexOutOfBoundException.
为什么它抛出NullPointerException而不是ArrayIndexOutOfBoundException。
Because you did not initialize the array. 因为您没有初始化数组。
Initialize array 初始化数组
static int x[] = new int[10];
Reason for NullPointerException : NullPointerException的原因:
Thrown when an application attempts to use null in a case where an object is required.
当应用程序在需要对象的情况下尝试使用null时抛出。 These include:
这些包括:
You hit by the bolder point, since the array is null
. 因为数组为
null
,所以您打了粗体。
It's throwing NullPointerException because x is null
. 因为
x is null
所以抛出NullPointerException 。
x[] is declared, but not initialized. x []已声明,但未初始化。
Before initialization, objects have null value, and primitives have default values (eg 0, false etc) 在初始化之前,对象具有空值,而基元具有默认值(例如0,false等)
So you should initialize as shown below: 因此,您应该如下所示进行初始化:
static int x[] = new int[20];
static int x [] = new int [20];
//at the time of declaration of x
or要么
static int x[];静态整数x [];
x = new int[20];x =新的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. 如果初始化数组并使用非法索引进行访问,则将发生ArrayIndexOutOfBoundException 。
eg :
例如:
x contains 20 elements, so index numbers 0 to 19 are valid, if we access with anyindex < 0
orx包含20个元素,因此,如果我们使用任何
index < 0
或
index > 19
, ArrayIndexOutOfBoundException will be thrown.index > 19
,将抛出ArrayIndexOutOfBoundException。
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 ). NullPointerException
在static block
抛出,您试图在该static block
中将值1分配给数组的第一个元素( x [0] = 1 )。 Be aware, the int[] array named x is still not intilized. 请注意,名为x的int []数组仍未使用。
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];
1使用
static int x[] = new int[5];
instead of static int x[] ;
而不是
static int x[] ;
2 2
Change 更改
static {
x[0] = 1;
}
To 至
static {
x= new int[5];
x[0] = 1;
}
Remember: Initialize the array before you use it.
切记:
Initialize the array before you use it.
You didn't initialize your x
array. 您没有初始化
x
数组。 There is a difference between declaration and initialization of variables. 变量的声明和初始化之间有区别。 When you write
int x[];
当你写
int x[];
you just declare a variable which, as an instance field, is initialized with a default value of null
. 您只需声明一个变量,该变量作为实例字段初始化为默认值
null
。 To actually create an array you must write int x[] = new int[10];
要实际创建一个数组,您必须编写
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. 获取
NullPointerException
而不是ArrayIndexOutOfBounds
的原因是,当您确实有一个数组并尝试解决其边界之外的位置时,后者将被抛出,但是在您的情况下,您根本没有数组并尝试放置一些东西放入一个不存在的数组。 That's why an NPE
这就是为什么
NPE
static int x[];
static {
x[0] = 1;
}
Results in NullPointerException, because your x array in not initialised (is null) 导致NullPointerException,因为您的x数组未初始化(为null)
ArrayIndexOutOfBoundException would happen if you accessed an index that is out of bounds: 如果您访问超出范围的索引,则会发生ArrayIndexOutOfBoundException:
static int x[] = new int[10];
static {
x[20] = 1; //<-----accessing index 20
}
It is simple. 很简单。 Here x is
null
and you are trying to store a value in uninitialized array
.Hence NullPointerException
这里x为
null
并且您试图将值存储在未初始化的array
。因此, NullPointerException
NullPointerException: This exception is thrown when you try to access the properties of an uninitialized object NullPointerException:当您尝试访问未初始化的对象的属性时,抛出此异常
ArrayIndexOutOfBoundsException: This exception is thrown when the array is initialized with an object but you try to access the array with invalid index. ArrayIndexOutOfBoundsException:当使用对象初始化数组但您尝试使用无效索引访问该数组时,将引发此异常。
In your case since you haven't initialized your object you are getting NullPointerException. 在您的情况下,由于尚未初始化对象,因此会得到NullPointerException。 You have created a person named "x" but have not associated any human being(array object).
您创建了一个名为“ x”的人,但尚未与任何人(数组对象)相关联。
If you change line 2 to, 如果将第2行更改为
static int x[] = new int[];
then you will get ArrayIndexOutOfBoundsException instead of NullPointerException. 那么您将获得ArrayIndexOutOfBoundsException而不是NullPointerException。
ExceptionInInitializerError is an Unchecked Exception. ExceptionInInitializerError是未经检查的异常。
While executing, the static block, static variable initialization, if any exception comes then it is ExceptionInInitializerError. 在执行时,将执行静态块,静态变量初始化,如果出现任何异常,则为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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.