I have the following line in a class, that I have written:-
private MathContext mc = new MathContext(Configurations.SCALE, RoundingMode.HALF_UP);
Now, when I run the class, I get the error "ExceptionInInitializerError". I know that the error is produced by this line, as when I replace the static variable 'Configurations,SCALE', with say, the number 8, then I no longer recieve it.
If you look at the stack trace, you'll probably see something blowing up in the initialization of a static member variable in Configurations. If you wrote that class, you can fix that error in Configurations. If you didn't, and you can't modify it, then your only choice is not to use it.
ExceptinInInitializerError happens when a class if loaded (initialized, actually, but for most purposes we can consider them to be a single operation). If a class you're trying to use fails to load, you can't use that class, and any code that depends on that class can't execute. So you either fix the class, or find a replacement.
Expanding on that a bit: the stack trace should have a stack trace for the ExceptionInInitializerError, followed by "caused by", followed by another exception stack trace that shows more detail about what happened. Possibly followed by another "caused by", and possibly more. Focus on the last "caused by" stack trace, as that's the root cause of your problems. If you don't see any "caused by", then you need to focus on how your errors are being logged, because you are losing valuable information. If you don't have a proper logging framework at least use e.printStackTrace() rather than System.out.println(e).
Joined: Jun 23, 2011
Thanks for your help, both of you. I did as you said, but sadly couldnt make much sense out of any of it. But, I re-jigged the class, by taking the SCALE variable out of the Configurations class, and adding it to the other class, that uses the MathContext line, as a static instance variable. This has seemed to work fine.
It would be good to learn how to diagnose these problems. It's not that hard, and it's a pretty fundamental and important skill. If you paste in your full stack trace, somebody here will be able to help you make sense of it. You won't get very far in Java programming if you don't learn to read a stack trace.