Java: ExceptionInInitializerError at program startup

When launched, my program immediately throws an ExceptionInInitializerError. Source from this method:

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

I printed different parts of the line and found that the error only occurs when form.format (date) is called. He says that it is zero. The only problem is that both the date and the form are statically declared directly above this method:

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

The error was suddenly launched after a small bug fix. I have no idea what is wrong or how something is wrong with this. I mean, I am invoking statically declared variables in the same class. Technically, they should not be zero, but they are. Anyone have any ideas why he 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" 

, A $.OpSystem.getOperatingSystem , A $.errorMessage...

, , ​​ , . , . , . ?

, , ...

EDIT: , , Cursor, "loadCursor" . ?

, ?

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");
}
}
+5
2

...

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

, A$ date form loadCursor, NullPointerException date form .

, , Cursor , date form. , §8.3.2.1 Java.

, §12.4.2.9, ...

, , , , .


, , , - :

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

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

loadCursor date form, , .


, (?), , . , , . ( ):

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) { }
}

.

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
        at Example.next(Example.java:11)
        at Example.<clinit>(Example.java:7)
+7

, , .

Thread.currentThread().getStackTrace()[3].getClassName() 

- ,

StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
String className = stackTrace[Math.min(3, stackTrace.length - 1)].getClassName();

: P

0

All Articles