-1

I am trying to do some database operations when a broadcast(CONNECTIVITY_CHANGE) happens, when my app is running its working fine, but after the app is killed it gives Exception :

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)

I recently started developing android apps, can anyone help me resolve this issue? Also any tips on optimizing my code will be appreciated. My code is this :

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);
    }
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Deepak Joshi
  • 151
  • 1
  • 15
  • that didn't help, – Deepak Joshi Sep 17 '16 at 06:57
  • Because you still don't know how to manage NPEs. Understand What an NPE is and why does it happen. Then you'll see the light. – Phantômaxx Sep 17 '16 at 11:03
  • 1
    I tried to look for this, after some time of searching i found the reason of NPE (Earlier i was sending activity, but when app is killed there wont be any activity which i didn't thought, but the broadcast receiver's restricted context will be there, so now i'm sending that, which solved my problem) , now i corrected it, thanks, but for learners(like me), u should be a little more detailed, Thanks anyways guys. – Deepak Joshi Sep 17 '16 at 11:09

1 Answers1

0

put all of your work in service and crate database instance in service

masoud vali
  • 1,528
  • 2
  • 18
  • 29