java.lang.ExceptionInInitializerError 导致:java.lang.NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20466906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:09:20  来源:igfitidea点击:

java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException

javaexceptionnullpointerexception

提问by Prashant Shilimkar

This is from OCJP example. I have written a following code

这是来自 OCJP 示例。我写了以下代码

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;

引起:java.lang.NullPointerException at x[0] = 1;

Why it is throwing NullPointerExceptionand not ArrayIndexOutOfBoundException.

为什么它是扔NullPointerException而不是ArrayIndexOutOfBoundException

采纳答案by Suresh Atta

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. These include:

当应用程序在需要对象的情况下尝试使用 null 时抛出。这些包括:

  • 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.
  • 调用空对象的实例方法。
  • 访问或修改空对象的字段。
  • 将 null 的长度当作一个数组。
  • 访问或修改 null 的插槽,就好像它是一个数组一样。
  • 将 null 视为 Throwable 值。

You hit by the bolder point, since the array is null.

您遇到了更大胆的点,因为数组是null.

回答by svz

You didn't initialize your xarray. 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.

你没有初始化你的x数组。变量的声明和初始化是有区别的。当您编写时,int x[];您只需声明一个变量,该变量作为实例字段使用默认值初始化null。要实际创建一个数组,您必须编写int x[] = new int[10];或您需要的大小。

The reason for getting a NullPointerExceptioninstead of ArrayIndexOutOfBoundsis 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

获得一个NullPointerException而不是的原因ArrayIndexOutOfBounds是当你有一个数组并试图解决一个超出其边界的位置时会抛出后者,但在你的情况下你根本没有一个数组并试图将一些东西放入一个不存在的数组。这就是为什么一个NPE

回答by MouseLearnJava

The NullPointerExceptionis 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.

在 中NullPointerException被抛出static block,您试图将值 1 分配给数组的第一个元素 ( x[0] = 1)。请注意,名为 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.

有 2 种方法可以修复它。

1 Use static int x[] = new int[5];instead of static int x[] ;

1 使用static int x[] = new int[5];代替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.

回答by Melquiades

static int x[];

static {
    x[0] = 1;
}

Results in NullPointerException, because your x array in not initialised (is null)

导致 NullPointerException,因为您的 x 数组未初始化(为空)

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
}

回答by Infinite Recursion

It's throwing NullPointerExceptionbecause x is null.

它抛出NullPointerException因为 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)

x[] 已声明,但未初始化。
初始化之前对象有空值,基元有默认值(例如 0、false 等)

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

static int x[] = new int[20]; //at the time of declaration of x

静态 int x[];
x = 新整数[20]; //after declaring x[] and before using x[] in your code

ArrayIndexOutOfBoundExceptionwill occur if array is initialized and accessed with an illegal index.

如果使用非法索引初始化和访问数组,则会发生ArrayIndexOutOfBoundException

e.g :
x contains 20 elements, so index numbers 0 to 19 are valid, if we access with any index < 0or
index > 19, ArrayIndexOutOfBoundException will be thrown.

例如:
x 包含 20 个元素,因此索引号 0 到 19 是有效的,如果我们使用任何index < 0or访问
index > 19,将抛出 ArrayIndexOutOfBoundException。

回答by Infinite Recursion

It is simple. Here x is nulland you are trying to store a value in uninitialized array.Hence NullPointerException

很简单。这里 x 是null,你正试图在未初始化的array.Hence 中存储一个值NullPointerException

回答by Sathesh

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. You have created a person named "x" but have not associated any human being(array object).

在您的情况下,由于您尚未初始化您的对象,因此您将收到 NullPointerException。您创建了一个名为“x”的人,但没有关联任何人(数组对象)。

If you change line 2 to,

如果您将第 2 行更改为

static int x[] = new int[];

then you will get ArrayIndexOutOfBoundsException instead of NullPointerException.

那么你会得到 ArrayIndexOutOfBoundsException 而不是 NullPointerException。

回答by Raman Gupta

ExceptionInInitializerErroris 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.