当应用程序被杀死时访问数据库会给出NPE

时间:2016-09-17 06:53:45

标签: android database sqlite nullpointerexception android-sqlite

我正在尝试在广播(CONNECTIVITY_CHANGE)发生时执行某些数据库操作,当我的应用程序运行正常时,但是在应用程序被终止后它会产生异常:

09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:  java.lang.NullPointerException
09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:     at com.example.deepak.broadcastrecieverswithdatabase.Operations.doInBackground(Operations.java:43)
09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:     at com.example.deepak.broadcastrecieverswithdatabase.Operations.doInBackground(Operations.java:14)
09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-17 12:10:01.185 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-17 12:10:01.185 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-17 12:10:01.185 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-17 12:10:01.185 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-17 12:10:01.185 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:     at java.lang.Thread.run(Thread.java:841)

我最近开始开发Android应用程序,任何人都可以帮我解决这个问题吗?此外,任何有关优化我的代码的提示将不胜感激。 我的代码是这样的:

package com.example.deepak.broadcastrecieverswithdatabase;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;

import java.util.ArrayList;
import java.util.List;


public class Operations extends AsyncTask<String, String, List<Integer>> {
DBHelper dbHelper;
SQLiteDatabase db;
Cursor cursor;
List<Integer> counterList;
public static final String DB_NAME = "db_name.db";
public static final int VERSION = 1;
public static final String TABLE = "counters";
public static final String COUNTER1 = "counter1";
public static final String COUNTER2 = "counter2";
String opType = "";
Context context = null;
String operation;
int counter1, counter2;
ContentValues contentValues;

public Operations(MainActivity activity, String operation, String opType, int counter1, int counter2){
    if(activity!=null)
        this.context = activity.getApplicationContext();
    this.operation = operation;
    this.counter1 = counter1;
    this.counter2 = counter2;
    this.opType = opType;
}

@Override
protected List<Integer> doInBackground(String... strings) {
    try {
        dbHelper = new DBHelper();
        db = dbHelper.getWritableDatabase();

        switch (operation) {
            case "insert":
                contentValues = new ContentValues();
                contentValues.put(COUNTER1, counter1);
                contentValues.put(COUNTER2, counter2);

                db.insertWithOnConflict(TABLE, null, contentValues, SQLiteDatabase.CONFLICT_IGNORE);
                break;

            case "update":
                counterList = new ArrayList<>();
                Cursor cursor1 = getCursor();
                try {
                    cursor.moveToFirst();
                    while (!cursor.isAfterLast()) {
                        counterList.add(Integer.parseInt(cursor.getString(cursor.getColumnIndex("counter1"))));
                        counterList.add(Integer.parseInt(cursor.getString(cursor.getColumnIndex("counter2"))));
                        cursor.moveToNext();
                    }
                } finally {
                    cursor.close();
                }
                counter1 = counterList.get(0);
                counter2 = counterList.get(1);
                if(opType.equals("alwaysOn"))
                {
                    counter1++;
                }
                else
                {
                    counter2++;
                }
                if (counter1 > 0) {
                    contentValues = new ContentValues();
                    contentValues.put(COUNTER1, counter1);
                    contentValues.put(COUNTER2, counter2);
                    db.update(TABLE, contentValues, COUNTER1 + "=" + (counter1 - 1), null);
                }
                List<Integer> counters = new ArrayList<>();
                counters.add(counter1);
                counters.add(counter2);
                return counters;

            case "get":
                counterList = new ArrayList<>();
                Cursor cursor = getCursor();
                try {
                    cursor.moveToFirst();
                    while (!cursor.isAfterLast()) {
                        counterList.add(Integer.parseInt(cursor.getString(cursor.getColumnIndex("counter1"))));
                        counterList.add(Integer.parseInt(cursor.getString(cursor.getColumnIndex("counter2"))));
                        cursor.moveToNext();
                    }
                    return counterList;
                } finally {
                    cursor.close();
                }
        }
    }catch (Exception npe)
    {npe.printStackTrace();} finally {

        dbHelper.close();
    }
    return null;
}

public Cursor getCursor(){
    cursor = db.query(TABLE, null, null, null, null, null, null);
    return cursor;
}

@Override
protected void onPostExecute(List<Integer> counterList) {
    super.onPostExecute(counterList);
    if(counterList!=null && context!=null) {
        MainActivity.alwaysOnValue.setText(""+counterList.get(0));
        MainActivity.onDemandValue.setText(""+counterList.get(1));
    }
}

class DBHelper extends SQLiteOpenHelper {

    public DBHelper() {
        super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql = String.format("create table %s"+"(%s int primary key, %s int)", TABLE, COUNTER1,COUNTER2);
        sqLiteDatabase.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        sqLiteDatabase.execSQL("drop table if exists "+ TABLE);
        onCreate(sqLiteDatabase);
    }
}
}

1 个答案:

答案 0 :(得分:0)

将您的所有工作投入使用并将数据库实例放入服务