10

In my Android app the WebView activity class has following line,

webView.addJavascriptInterface(new JSInterface(this), "Android");   

And in JSInterface class, I'm initializing Google "SpreadSheetService" like below,

import com.google.gdata.client.spreadsheet.SpreadsheetService;

--- some more imports ---


public class JSInterface {
    Context mContext;

    public SpreadsheetService service;

    /** Instantiate the interface and set the context */
    JSInterface(Context c) {
        mContext = c;
        service = new SpreadsheetService("List Demo");
    }

    ------- some more code -----

When I run the application I'm getting the following exception,

01-19 21:38:00.652: E/AndroidRuntime(4085): java.lang.ExceptionInInitializerError

which has the below trace

01-19 21:38:00.652: E/AndroidRuntime(4085): FATAL EXCEPTION: main
01-19 21:38:00.652: E/AndroidRuntime(4085): java.lang.ExceptionInInitializerError
01-19 21:38:00.652: E/AndroidRuntime(4085):     at com.android.quotes.JSInterface.<init>(JSInterface.java:33)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at com.android.quotes.CHQuotesActivity.onCreate(CHQuotesActivity.java:19)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.Activity.performCreate(Activity.java:4465)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.os.Looper.loop(Looper.java:137)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.ActivityThread.main(ActivityThread.java:4340)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at java.lang.reflect.Method.invoke(Method.java:511)

I searched Google, but didnt get any solution for this.. Any idea on why I'm getting this exception??

Peter

Naveen
  • 1,040
  • 4
  • 15
  • 38
  • Is it possible you create an asyncTask/Handler anywhere in the code? – android developer May 19 '15 at 12:23
  • Seems the **trace** shows only where the `exception` is happening at. But does not show what it is causing it, from the exception find this line `Caused by:`. Check the line which causes the error. – 4xMafole Jan 21 '21 at 00:14

5 Answers5

17

Based on this documentation An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable. Check your code has any static initialization logic.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • Yeah, but what could be the problem? A static member should have no issues being accessed, hence it is static. – IgorGanapolsky Sep 11 '13 at 01:59
  • 2
    @IgorGanapolsky: It is not due to being accessed, it is because of value being initialized is not properly constructed. The right side part of static variable. – kosa Sep 11 '13 at 03:15
  • Interesting, initialized to what? An inconvertible type or null? – IgorGanapolsky Sep 11 '13 at 04:44
  • 3
    @IgorGanapolsky: My guess is, some class instance, which is either throwing exception (or) causing some bad dependency. – kosa Sep 11 '13 at 05:49
  • I am using a library in my app. For specific class in that library, whenever I create a new instance of it, my app crashes and gives me the same exception. Since it is a library class, it means I cannot change any thing in it. At the same time, I want to use it is functionality inside that class's methods. I am totally stuck and cannto find a way to solve this, any suggestions ? thanks – McLan Feb 15 '17 at 11:04
  • @Suda.nese I would suggest create new question and refer this question, you could get lot of answers. – kosa Feb 15 '17 at 15:54
  • 1
    @Nambari : I've already done so here : http://stackoverflow.com/q/42227184/2135363 .. and here : http://stackoverflow.com/q/42248119/2135363 .. Please have a look. – McLan Feb 15 '17 at 16:21
  • @McLan did you found a solution to your problem? – user158 Oct 19 '19 at 08:17
  • @user158 can’t remember. But will check for you. – McLan Oct 19 '19 at 09:14
1

java.lang.ExceptionInInitializerError android OS 11

if this is related to OkHttp then update your version 4.4.0.

In this version its fixed.

implementation 'com.squareup.okhttp3:logging-interceptor:4.4.0'

thanks.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Yogendra
  • 4,817
  • 1
  • 28
  • 21
1

If your android version is 11 or higher you might encounter java.lang.ExceptionInInitializerError and IllegalStateException for which you will need to add latest okhttp3 dependencies which are:

implementation 'com.squareup.okhttp3:okhttp:4.9.1'  

and

implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
Ryan M
  • 18,333
  • 31
  • 67
  • 74
SaminSyed
  • 85
  • 6
0

Strange! But let me share my experience, maybe it can help someone!

Was working on an Android app that was previously being run without any issues. After I rest my Android device and on the first install of App after the reset. I got this exception java.lang.ExceptionInInitializerError

  1. I uninstalled the app
  2. Cleaned the project
  3. Rebuild the project

After that when I ran the app the exception was gone.

I don't know what happened, but the above steps just solved the issue.

Muhammad waris
  • 314
  • 1
  • 10
0

The ExceptionInInitializerError (updated link) shows that an unexpected exception has occurred in a static initializer. Literally, if this exception is shown, one should understand that Java has failed to do evaluation on static initializer block or on instantiation of a static variable.

Anyways, each moment static initializer encounters an exception its automatically wrapped into an instance of ExceptionInInitializerError class. In such a way it keeps a reference to the actual exception as the root cause.

On my situation, using below exception trace:

   java.lang.ExceptionInInitializerError
        at com.fole_Studios.sup.database.DBqueries.getAllUniversities(DBqueries.java:581)
        at com.fole_Studios.sup.RegisterFragment.universityDataList(RegisterFragment.java:216)
         ...............
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
        at com.fole_Studios.sup.database.DBqueries.<clinit>(DBqueries.java:80)

I referred directly from what caused the error from Caused by:. Hence, I discovered that I am instantiating a static variable which has a null value. I solved it and everything was working perfect.

4xMafole
  • 479
  • 1
  • 8
  • 20