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