NullPointerException crash reports using static Da

2019-09-10 02:52发布

I’m receiving random NullPointerException crash reports from Google that can’t be reproduced. I’m using a static reference to a class that extends OrmLiteSqliteOpenHelper. The helper is initialised in the main class that extends SherlockFragmentActivity.

if (DatabaseHelper.getInstance() == null) {
    DatabaseHelper.setInstance(this.getApplicationContext());
}

At first I though it may occur when the device is rotated or sent to the background. But no amount of testing has been able to recreate the issue.

Nowhere in the code is the static mHelper variable set to null

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
    …
    private static DatabaseHelper mHelper;
    …
    public static DatabaseHelper getInstance() {

        return DatabaseHelper.mHelper;
    }

    public static void setInstance(Context context) {

        DatabaseHelper.mHelper = new DatabaseHelper(context);
    }
    …
}

Does anyone have any idea of what may be causing this?

Follows is one of the reports:

java.lang.RuntimeException: Unable to start activity ComponentInfo{au.com.dcbs.tradies/au.com.dcbs.tradies.presentation.document.DocumentActivity}: java.lang.NullPointerException
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2083)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4697)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at bd.a(Document.java:654)
at au.com.dcbs.tradies.presentation.document.DocumentActivity$a.b(DocumentActivity.java:102)
at au.com.dcbs.tradies.presentation.document.DocumentActivity$a.onActivityCreated(DocumentActivity.java:78)
at e.a(FragmentManager.java:891)
at e.a(FragmentManager.java:1080)
at e.a(FragmentManager.java:1062)
at e.k(FragmentManager.java:1810)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:501)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
at android.app.Activity.performStart(Activity.java:4549)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2022)
... 11 more

line 654 is the first line in this method:

public static Document get(long id, Context context) {

    DocumentEntity entity = DatabaseHelper.getInstance().getDocumentDao().queryForId((int)id);
    Document doc = new Document(entity, context);
    return doc;
}

getDocumentDao returns an object of type com.j256.ormlite.dao.RuntimeExceptionDao

2条回答
Emotional °昔
2楼-- · 2019-09-10 03:21

It would help if you post the code where the problem occurs

at bd.a(Document.java:654)

But as general suggestion that might resolve your problem, you can consider initializing the database helper in an application class not in any activity, since it seems that it is something used for the entire application and does not depend on specific activity context.

Edit

I think you need to break the code at line 654 because it is difficult to tell which function returns null. e.g.

DatabaseHelper helper = DatabaseHelper.getInstance();
...
查看更多
疯言疯语
3楼-- · 2019-09-10 03:43

In Android, it does not ensure static field variable is not always kept, you should...

public static DatabaseHelper getInstance(Context context) {
    if (mHelper == null) {
        mHelper = new DatabaseHelper(context);
    }
    return mHelper;
}
查看更多
登录 后发表回答