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");
}
}
After examining your exception trace…
It becomes clear that some static field initialization in
A$is executing prior to the initialization ofdateandformand callingloadCursor, which then logically fails withNullPointerExceptionasdateandformare uninitialized.The issue is that you’ve placed the the code which initializes your
Cursorbefore that which initializes yourdateandformobjects. 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…
So, you’re likely doing something like this:
loadCursormust not be called beforedateandformare 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):
The output can be seen to be as follows.