Prashant Shilimkar
Prashant Shilimkar

Reputation: 8830

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

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

Answers (8)

Raman Gupta
Raman Gupta

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

Suresh Atta
Suresh Atta

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:

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

Upvotes: 12

Infinite Recursion
Infinite Recursion

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 any index < 0 or
index > 19, ArrayIndexOutOfBoundException will be thrown.

Upvotes: 3

Sathesh
Sathesh

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

user3099347
user3099347

Reputation:

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

Upvotes: 1

Mengjun
Mengjun

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

Melquiades
Melquiades

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

svz
svz

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

Related Questions