Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: ExceptionInInitializerError on program startup

On start up, my program immediately throw an ExceptionInInitializerError. The source is from this method:

public static void errorMessage(String input) {
    System.err.println("[ERROR] " + form.format(date) + " - " + Thread.currentThread().getStackTrace()[3].getClassName() + ": " + input);
}

I printed out the different parts of the string and found that the error is only thrown when I call form.format(date). It says it is null. The only problem is, both date and form are statically declared right above this method as so:

public static Date date = new Date();
public static DateFormat form = new SimpleDateFormat("HH:mm:ss");

The error suddenly started being thrown after some minor bug fixing. I have no idea what is wrong or how something could be wrong with this. I mean, I am calling on statically declared variables in the same class. Technically, they should not be null, yet they are. Anyone have any ideas why it is throwing this error? Here is the console output:

java.lang.ExceptionInInitializerError
at A$$OpSystem.getOperatingSystem(A$.java:98)
at A_.<clinit>(A_.java:19)
Caused by: java.lang.NullPointerException
at A$.errorMessage(A$.java:72)
at A$.loadCursor(A$.java:84)
at A$.<clinit>(A$.java:62)
... 2 more
Exception in thread "main" 

By the way, A$.OpSystem.getOperatingSystem is only being shown there because it calls A$.errorMessage...

And I have had this problem before, it was just when a statically declared variable was actually never declared an remained null when it was called. Now it is not supposed to be null, yet it is. So I have no idea what is causing it. Ideas?

But I guess this is a good time to be educated on how static variables actually load...

EDIT: It seems as though no exception is thrown if I move the static Cursor object that calls 'loadCursor' to a different class. What?

I made a test of this situation but it returns no error?

public class StaticMethodTesting {

public static int i = getInt();

public static int getInt() {
    return getAnotherInt();
}

public static int getAnotherInt() {
    return 0;
}

public static void main(String[]args) {
    System.out.println("Hi");
}
}
like image 413
CoderTheTyler Avatar asked Aug 15 '12 03:08

CoderTheTyler


1 Answers

After examining your exception trace...

at A$.errorMessage(A$.java:72)
at A$.loadCursor(A$.java:84)
at A$.< clinit>(A$.java:62)

It becomes clear that some static field initialization in A$ is executing prior to the initialization of date and form and calling loadCursor, which then logically fails with NullPointerException as date and form are uninitialized.

The issue is that you've placed the the code which initializes your Cursor before that which initializes your date and form objects. Static fields with assignments at declaration time are initialized in declaration order, as per Section §8.3.2.1 of the Java Language Specification.

If you read the detailed initialization process, particularly Section §12.4.2.9, you find...

Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.


So, you're likely doing something like this:

static Cursor cursor = loadCursor();
static Date date = new Date();
static DateFormat form = new SimpleDateFormat("HH:mm:ss");

static Cursor loadCursor() {
  ...
  errorMessage("...");
  ...
}

loadCursor must not be called before date and form are initialized if you wish for this to work.


The reason your example produces no 'error' (huh?) is because neither method refer to a yet uninitialized field. If you want equivalent behavior which is in no way an error, see the following (which can be seen run here):

import java.util.Random;

public final class Example {

  /* note if the below read: static final int value = rand.nextInt(),
     this would be considered an illegal forward reference to rand */
  private static final int value = next();
  private static final Random rand = new Random();

  private static int next() {
    return rand.nextInt();
  }

  public static void main(final String[] argv) { }
}

The output can be seen to be as follows.

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
        at Example.next(Example.java:11)
        at Example.<clinit>(Example.java:7)
like image 114
obataku Avatar answered Nov 10 '22 00:11

obataku