-1

i have this problems in logcat:

java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224);
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188);
at package.SQLiteAdapter.openToRead(SQLiteAdapter.java:52);
at package.SMSStateReceiver.cercaNumeroSQLite(SMSStateReceiver.java:99);

I know with debugger that this problem comes from the context null of constructor! I m on this problem for 3 days and although various attempts, not having much experience with Android, I can not find a suitable manufacturer to resolve the issue!

I have this SQLite Adapter that contains String name at content1 and String phone_number at content2 that work find....

public class SQLiteAdapter {

 public static final String MYDATABASE_NAME = "MY_DATABASE";
 public static final String MYDATABASE_TABLE = "MY_TABLE";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_ID = "_id";
 public static final String KEY_CONTENT1 = "Content1";
 public static final String KEY_CONTENT2 = "Content2";

 private String[] STUDENT_TABLE_COLUMNS = { KEY_ID, KEY_CONTENT1, KEY_CONTENT2 };

 //create table MY_DATABASE (ID integer primary key, Content text not null);
 private static final String SCRIPT_CREATE_DATABASE =
  "create table " + MYDATABASE_TABLE + " ("
  + KEY_ID + " integer primary key autoincrement, "
  + KEY_CONTENT1 + " text not null, "
  + KEY_CONTENT2 + " text not null);";

 private SQLiteHelper sqLiteHelper;
 private SQLiteDatabase sqLiteDatabase;

 private Context context;

 private SMSStateReceiver sms;

 public SQLiteAdapter(Context c){
  context = c;
 }


public SQLiteAdapter(SMSStateReceiver smsStateReceiver) {
    // TODO Auto-generated constructor stub
}


public SQLiteAdapter openToRead() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  return this;
 }

 public SQLiteAdapter openToWrite() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  return this;
 }

 public void close(){
  sqLiteHelper.close();
 }

 public long insert(String content1, String content2){

  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_CONTENT1, content1);
  contentValues.put(KEY_CONTENT2, content2);
  return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
 }

 public int deleteAll(){
  return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
 }

 public void delete_byID(int id){
  sqLiteDatabase.delete(MYDATABASE_TABLE, KEY_ID+"="+id, null);
 }

 public void update_byID(int id, String v1, String v2){
  ContentValues values = new ContentValues();
  values.put(KEY_CONTENT1, v1);
  values.put(KEY_CONTENT2, v2);
  sqLiteDatabase.update(MYDATABASE_TABLE, values, KEY_ID+"="+id, null);
 }

 public Cursor queueAll(){
  String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2};
  Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
    null, null, null, null, null);

  return cursor;
 }


    public List getAllNumeri() {
        List students = new ArrayList();

        Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, STUDENT_TABLE_COLUMNS, null, null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Persona student = parseStudent(cursor);
            students.add(student);
            cursor.moveToNext();
        }

        cursor.close();
        return students;
    }
    private Persona parseStudent(Cursor cursor) {
        Persona student = new Persona();
        student.setName(cursor.getString(2));
        return student;
    }   

 public class SQLiteHelper extends SQLiteOpenHelper {

  public SQLiteHelper(Context context, String name,
    CursorFactory factory, int version) {
   super(context, name, factory, version);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   // TODO Auto-generated method stub
   db.execSQL(SCRIPT_CREATE_DATABASE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO Auto-generated method stub

  }

    public List getAllStudents() {
        List students = new ArrayList();

        Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, STUDENT_TABLE_COLUMNS, null, null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Persona student = parseStudent(cursor);
            students.add(student);
            cursor.moveToNext();
        }

        cursor.close();
        return students;
    }

    private Persona parseStudent(Cursor cursor) {
        Persona student = new Persona();
        student.setId((cursor.getInt(0)));
        student.setName(cursor.getString(1));
        return student;
    }    

 }

}

I have also a sms receiver, and i m trying to compare incoming sms number with the list of numbers in sqlite:

public class SMSStateReceiver extends BroadcastReceiver 
{

     private SQLiteAdapter mySQLiteAdapter;
     Cursor c;
     CallFilterService cfs;

    public void onReceive(Context context, Intent intent) 
    { 
        String MSG_TYPE=intent.getAction();


         if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED"))
        {
             Toast toast;
             Toast toast = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);
             toast.show();

            Bundle bundle = intent.getExtras();
            Object messages[] = (Object[]) bundle.get("pdus");
            SmsMessage smsMessage[] = new SmsMessage[messages.length];
            for (int n = 0; n < messages.length; n++) 
            {
                smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
            }

              final SharedPreferences mpref=context.getSharedPreferences("BLOCK",context.MODE_PRIVATE);
              String str=mpref.getString("phonesms","Phone");
              Log.e("phone no==",""+smsMessage[0].getOriginatingAddress());

              boolean value = cercaNumeroSQLite(smsMessage[0].getOriginatingAddress().toString());

              if(str.contains("Phone") && (value == true))    
              {
                    toast = Toast.makeText(context,"BLOCKED Received SMS from: " +smsMessage[0].getOriginatingAddress(), 5000);
                    toast.show();
                    abortBroadcast();
              }


        }
        else if(MSG_TYPE.equals("android.provider.Telephony.SEND_SMS"))
        {

        }
        else
        {

            Toast toast = Toast.makeText(context,"SIN ELSE: "+MSG_TYPE , Toast.LENGTH_LONG);
            toast.show();
            abortBroadcast();
            for(int i=0;i<8;i++)
            {
                System.out.println("Blocking SMS **********************");
            }

        }

    }

    public boolean cercaNumeroSQLite(String phone) {
        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToRead();

        System.out.println("num tel - " + phone);

        List numeri = mySQLiteAdapter.getAllNumeri();
        String num = numeri.toString();

        boolean trovato = false;

        if(numeri.size() >= 1) {
            List<String> numeriTel = new ArrayList<String>();  
            numeriTel = mySQLiteAdapter.getAllNumeri();
            for (int i = 0; i < numeri.size(); i++) {
                Persona numero = (Persona) numeri.get(i);
                if(PhoneNumberUtils.compare(numero.toString(), phone)){
                     trovato = true;
                     mySQLiteAdapter.close();
                } else {

                }
            }
        } else {
            mySQLiteAdapter.close();
        }

            return trovato;
        }

 }

Someone can help me??? Thanks for help me

Dance F
  • 13
  • 1
  • 5
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Dec 10 '14 at 14:55

2 Answers2

0

Check weather the context your passing to the Consturctor of SQLiteAdapter is same as you receive on the onReceive method in BroadcastReceiver.

u.jegan
  • 833
  • 6
  • 15
0

Your context variable in SQLiteAdapter is not initialized. You have two constructors and only one of them initializes the variable. The one you're actually using doesn't initialize it.

Passing a null Context to SQLiteOpenHelper constructor causes this exception when the database is accessed with e.g. getWritableDatabase().

To fix it, remove this constructor:

public SQLiteAdapter(SMSStateReceiver smsStateReceiver) {
    // TODO Auto-generated constructor stub
}

and change

mySQLiteAdapter = new SQLiteAdapter(this);

to something like

mySQLiteAdapter = new SQLiteAdapter(context);

where context is the Context passed to your onReceive().

laalto
  • 150,114
  • 66
  • 286
  • 303