Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExceptionInInitializerError While database is initializing #424

Closed
Ajibola opened this issue Nov 13, 2015 · 18 comments
Closed

ExceptionInInitializerError While database is initializing #424

Ajibola opened this issue Nov 13, 2015 · 18 comments

Comments

@Ajibola
Copy link

Ajibola commented Nov 13, 2015

We have been using Sugar ORM for a while now and have an application in production with it. We have a table Stats that was not being setup and this caused that part of the code to throw errors. Now i can't seem to get anything to work, getting an ExceptionInInitializerError while the database is trying to initialize when the app launches for the first time. This error causes the app to crash.

Was using version 1.4 beta, now migrated to 1.4 release but still same error. I have uninstalled the app and cleared the app data on device, but this doesn't help at all. Please do anyone have an idea what is going on?

The app is currently using multidex, not sure how this could affect the db though.

ava.lang.ExceptionInInitializerError
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime: at java.lang.Class.classForName(Native Method)
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime: at java.lang.Class.forName(Class.java:251)
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime: at com.orm.util.ReflectionUtil.getDomainClass(ReflectionUtil.java:273)
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime: at com.orm.util.ReflectionUtil.getDomainClasses(ReflectionUtil.java:257)
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime: at com.orm.SchemaGenerator.createDatabase(SchemaGenerator.java:42)
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime: at com.orm.SugarDb.onCreate(SugarDb.java:26)
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime: at com.orm.SugarDb.getDB(SugarDb.java:36)
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime: at com.orm.SugarRecord.getSugarDataBase(SugarRecord.java:34)
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime: at com.orm.SugarRecord.findWithQuery(SugarRecord.java:161)
11-13 23:32:01.924 18015-18015/ng.amebo E/AndroidRuntime:

@JusticeRage
Copy link

I'm encountering the same bug, on multiple 4.0.3 and 4.0.4 devices.
Here is a sample stacktrace (I have many others if needed):

java.lang.ExceptionInInitializerError
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:217)
at com.orm.util.ReflectionUtil.getDomainClass(ReflectionUtil.java:270)
at com.orm.util.ReflectionUtil.getDomainClasses(ReflectionUtil.java:254)
at com.orm.SchemaGenerator.createDatabase(SchemaGenerator.java:37)
at com.orm.SugarDb.onCreate(SugarDb.java:26)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
at com.orm.SugarDb.getDB(SugarDb.java:36)
at com.orm.SugarRecord.find(SugarRecord.java:189)
at com.orm.SugarRecord.find(SugarRecord.java:158)
at fr.kwiatkowski.apktrack.model.InstalledApp.find_app(InstalledApp.java:201)
at fr.kwiatkowski.apktrack.model.InstalledApp.detect_new_version(InstalledApp.java:160)
at fr.kwiatkowski.apktrack.MainActivity$1.run(MainActivity.java:80)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607)
at android.os.AsyncTask.<clinit>(AsyncTask.java:190)
... 14 more
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607)
at android.os.AsyncTask.<clinit>(AsyncTask.java:190)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:217)
at com.orm.util.ReflectionUtil.getDomainClass(ReflectionUtil.java:270)
at com.orm.util.ReflectionUtil.getDomainClasses(ReflectionUtil.java:254)
at com.orm.SchemaGenerator.createDatabase(SchemaGenerator.java:37)
at com.orm.SugarDb.onCreate(SugarDb.java:26)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
at com.orm.SugarDb.getDB(SugarDb.java:36)
at com.orm.SugarRecord.find(SugarRecord.java:189)
at com.orm.SugarRecord.find(SugarRecord.java:158)
at fr.kwiatkowski.apktrack.model.InstalledApp.find_app(InstalledApp.java:201)
at fr.kwiatkowski.apktrack.model.InstalledApp.detect_new_version(InstalledApp.java:160)
at fr.kwiatkowski.apktrack.MainActivity$1.run(MainActivity.java:80)
at java.lang.Thread.run(Thread.java:856)

I think the problem comes from SugarORM. Normally, I'm using the latest version of the code (11e8d74).

@Ajibola
Copy link
Author

Ajibola commented Nov 16, 2015

It appears there is a fatal error while the DB is initializing, this causes the DB to be corrupted for the user. Not sure if it is purely from SugarDB or from 3rd party framework.

But it appears the error is on DB update. Right now i am trying to delete existing DBs where this error occurs.

@leberrm
Copy link

leberrm commented Nov 23, 2015

Hello,
I have exactly the same error when I launch my application on my Android 4.4.2 but it works fine with my 6.0 device. The error occurs when calling product.save(); Did you find anything ? I also have the stacktrace if needed but it is really similar to the ones above.

@leberrm
Copy link

leberrm commented Nov 24, 2015

Good news ! I replaced this line in my Manifest :
<meta-data android:name="VALUE" android:value="2"
by this :
<meta-data android:name="VALUE" android:value="1"
and also added :
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example.megan.sugartest" (which is supposed to be optional in SugarORM doc)
and now it works fine !

@Ajibola
Copy link
Author

Ajibola commented Nov 24, 2015

currently sorting this out by deleting existing database file and letting it be reinitialized on the first call to sugar orm. It seems the error happens on upgrade.

String[] dbArray = this.databaseList();

        for (int i = 0; i < dbArray.length; i++) {
            String dbName = dbArray[i];

            if (dbName.contains("mydb"))
                this.deleteDatabase(dbName);
        }

@JusticeRage
Copy link

I guess that would work, but deleting all data on error seems like bad user experience to me.

@nicolabeghin
Copy link

I think it's useful to take a look at issue #391

@Ajibola
Copy link
Author

Ajibola commented Dec 4, 2015

@nicolabeghin you are right, currently adding domain package name solves the error, but testing with master branch with fix for multidex support, still got the error.

@sibelius
Copy link
Contributor

sibelius commented Dec 4, 2015

@Ajibola could you post your logs? I do not have an Android 6.0 to test right now

@nicolabeghin
Copy link

@sibeliusseraphini in my case this happens on < 6.0. I can confirm it doesn't work on android 4.3 and 4.4 when compiling with targetSdk 23 and support library 23.0.1

@sibelius
Copy link
Contributor

sibelius commented Dec 4, 2015

@nicolabeghin maybe this PR #362, could fix it, could you test it please?

@nicolabeghin
Copy link

thanks @sibeliusseraphini! I can confirm it solves the issue.
this is my build.gradle.

dependencies {
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:cardview-v7:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'de.hdodenhof:circleimageview:1.3.0'
    compile 'com.android.support:support-v4:23.0.1'
    compile 'com.google.code.ksoap2-android:ksoap2-android:3.4.0'
    compile 'joda-time:joda-time:2.8.1'
    compile 'com.mobsandgeeks:android-saripaar:2.0-SNAPSHOT'
    compile 'com.splunk.mint:mint:4.4.0'
    compile files('libs/sendmelogs-0.1.jar')
    compile project(':sugarorm')
    compile project(':countrypickerlib')
    compile 'org.apache.commons:commons-io:1.3.2'
}

@husyn
Copy link

husyn commented Dec 7, 2015

I've tried
compile 'com.github.satyan:sugar:f8eb02eb7671538054ec88bb0f408fa3532e9fe1'
compile 'com.github.satyan:sugar:1.3'
compile 'com.github.satyan:sugar:1.4'

tried with DOMAIN_PACKAGE_NAME and without it. I'm on 4.4.2 and this library is not working for me in any case 👎

@sibelius
Copy link
Contributor

sibelius commented Dec 7, 2015

fixed with #362

@sibelius sibelius closed this as completed Dec 7, 2015
@adristanchev
Copy link

@leberrm
This worked for me:
<meta-data android:name="VALUE" android:value="1"
and also added :
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example.your.package.name"
So, thanks :)

Looks like this issue has not yet been fixed.

@rezanahardani
Copy link

here is resolved...
https://stackoverflow.com/a/44619474/7577642

@jamesgathu
Copy link

jamesgathu commented Dec 13, 2017

This maybe late but I have found an issue with the way objects are compared in Android versions lower than LOLLIPOP within Sugar ORM
For your models (Classes extending SugarRecord) on Android Versions lower than LOLLIPOP, Change the equals(...) method if you have overriden it to something like:

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public boolean equals(Object o) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // your logic for confirming equality
        }
       return super.equals(o);
   }

@rezanahardani
Copy link

rezanahardani commented Dec 14, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants