Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:39:52+00:00 2026-06-09T20:39:52+00:00

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

  • 0

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");
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:39:53+00:00Added an answer on June 9, 2026 at 8:39 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As we all know Java program will start executing from the public static void
I discovered very interested issue with XamDataGrid LoadCustomizations method. At start program I initialize
i am trying to start the Program D from the terminal, command in terminal
I wish to retrieve a unique number every time I start my program (from
I'm trying to compile this simple program to start learning how to use timers:
Here is my scenario: Start my test program and immediately break into the debugger
My program moves files from one folder to another immediately upon creation. The files
The program’s code is like below: using System.Diagnostics; class Test { static void Main()
I'm a starting C/C++ programmer. What I want is the following thing: Start program,
When I start the program, I get the dialog and everything. But it closes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.