简体   繁体   中英

App Crashing on Start Up - Null pointer

I am getting this LogCat from an app that resumes (after the Activity has long been gone (not everytime). It is actually a specific Fragment , so it's when the FragmentActivity resumes.

12-18 18:45:33.315: E/AndroidRuntime(7593): FATAL EXCEPTION: AsyncTask #1
12-18 18:45:33.315: E/AndroidRuntime(7593): java.lang.RuntimeException: An error occured while executing doInBackground()
12-18 18:45:33.315: E/AndroidRuntime(7593):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-18 18:45:33.315: E/AndroidRuntime(7593):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-18 18:45:33.315: E/AndroidRuntime(7593):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-18 18:45:33.315: E/AndroidRuntime(7593):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-18 18:45:33.315: E/AndroidRuntime(7593):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-18 18:45:33.315: E/AndroidRuntime(7593):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-18 18:45:33.315: E/AndroidRuntime(7593):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-18 18:45:33.315: E/AndroidRuntime(7593):     at java.lang.Thread.run(Thread.java:856)
12-18 18:45:33.315: E/AndroidRuntime(7593): Caused by: java.lang.NullPointerException
12-18 18:45:33.315: E/AndroidRuntime(7593):     at com.---.---.DebtDataSource.payoffDebt(DebtDataSource.java:229)
12-18 18:45:33.315: E/AndroidRuntime(7593):     at com.---.---.PlannerFragment$PlannerTask.doInBackground(PlannerFragment.java:131)
12-18 18:45:33.315: E/AndroidRuntime(7593):     at com.---.---.PlannerFragment$PlannerTask.doInBackground(PlannerFragment.java:1)
12-18 18:45:33.315: E/AndroidRuntime(7593):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-18 18:45:33.315: E/AndroidRuntime(7593):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-18 18:45:33.315: E/AndroidRuntime(7593):     ... 4 more

The Main Activity has this:

@Override
protected void onDestroy() {
    super.onDestroy();
    datasource.close();
    finish();

}

Nothing in the onResume() .

The Fragment calls an AsyncTask when the Activity has been in the background after along time.

The problem is DoInBackground base on the Log. ( PlannerFragment )

@Override
    protected Void doInBackground(String... params) {


    if (paymentTotal > allFee) {

        totalMonths = datasource.payoffDebt(Double.valueOf(columnDebtTotal)); // this is line 131
        finalDate = datasource.getDate(totalMonths);
        allFeeLife = datasource.getLiftimeFees();

    }

    return null;

}

Then the Nullpointer is after Nullpointer tries to grab data from my SQLite DB.

( DebtDataSource Class)

public int payoffDebt(Double totalDebt) {
Cursor c = null;

if (DebtPlanner.PlanType.equals("highint")) { // line 229 -- NULL CRASH!
    c = database.rawQuery("SELECT *  FROM debt ORDER BY CAST("
            + MySQLiteHelper.COLUMN_APR + " as integer) DESC;", null);
}

Again, this only crashes when the Activity resumes after a long time in the background (long enough to require the task above in the onActivityCreated to be called again).

NullPointerException is thrown when executing: DebtPlanner.PlanType.equals("highint") . There are two possible reasons:

(1) DebtPlanner is null , or (2) PlanType is null

Solution Verify that both these fields have been initialized and are not null when this line is executed. That should fix the problem.

对于更长的操作, LoaderManager和AsyncTaskLoader优于AsyncTask。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM