title: Null pointer Exception in Broadcast receiver while accessing DB
body: <p>i want to access phone number from my DB in Broadcast receiver to compare with message address i got null pointer exception in SMSlockDB and SMsReciver class i point out that also in my code because Data base does not open i also check my method in other activities it work correctly the phone number also save in Data Base 
  here  is my code </p>
  
  <pre><code>public class SmsReciever extends BroadcastReceiver {
  
  SQLiteDatabase database;
  //Context c;
  
  @Override
  public void onReceive(Context context, Intent intent) {
      // TODO Auto-generated method stub
      // ---get the SMS message passed in---
       SmslockDB no = new SmslockDB(context);
  
      database = context.openOrCreateDatabase(SmslockDB.DATABASE_NAME, 0, null);
  
          Bundle bundle = intent.getExtras();
          SmsMessage[] msgs = null;
          String adrrs = "";
          String msbdy = "";
          String messages = "";
          if (bundle != null) {
              // ---retrieve the SMS message received---
              Object[] pdus = (Object[]) bundle.get("pdus");
  
              msgs = new SmsMessage[pdus.length];
              for (int i = 0; i &lt; msgs.length; i++) {
                  msgs[i] = SmsMessage.createFromPdu((byte[]) 
   pdus[i]);
   adrrs += msgs[i].getOriginatingAddress().toString().trim();
          msbdy += msgs[i].getMessageBody().toString().trim();
          String rmspno = adrrs.replaceAll("\\s+", "");
          String cmpno = no.Spclgetno(rmspno);&lt;------------ here my loggate  
                                                                            show error
  
                  if (rmspno.contains(cmpno)) {
  
                      messages += "SMS from " + n + " :\n";
                      messages += msbdy + "\n";
  
                      try {
  
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
  
                      // ---display the new SMS message--
          Toast.makeText(context, messages, Toast.LENGTH_SHORT)
                              .show();
                      this.abortBroadcast();
  
                  }
              }
  
          }
      }
         }
  </code></pre>
  
  <p>here is my DB</p>
  
  <pre><code>public class SmslockDB {
  
  //  tabel columns
  public static final String ROWID_PRF = " _id";
  public static final String PRF_NO = "_noto";
  public static final String PRF_NAME = "_name";
  
  //  DB Name
  public static final String DATABASE_NAME = "SMSlock_DB";
  //  DB version
  private static final int DATABASE_VERSION = 1 ;
      // table 1 name 
  private static final String TABLE_PROFILE = "T_SProle";   // second table name
  
  private DbHelper ourhelper;
  private Context ourcontext;
  private SQLiteDatabase ourdatabase;
  public static class DbHelper extends SQLiteOpenHelper{
  
      public Context context;
      public DbHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
          // TODO Auto-generated constructor stub
          this.context = context ;
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          // TODO Auto-generated method stub
          db.execSQL("CREATE TABLE " + TABLE_MSSG +"(" +
                  ROWID_SM + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                  SMS_NO + " TEXT NOT NULL, " +
                  SMS_Body + " TEXT NOT NULL );"
                  );
          db.execSQL("CREATE TABLE " + TABLE_PROFILE +"(" +
                  ROWID_PRF + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                  PRF_NO + " TEX NOT NULL, " +
                  PRF_NAME + " TEXT NOT NULL );"
                  );  
         }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // TODO Auto-generated method stub
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_MSSG);
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROFILE);
          onCreate(db);
  
      }
      }
   public SmslockDB(Context c) {
  // TODO Auto-generated constructor stub
      ourcontext = c;
  }
   public SmslockDB open() throws SQLException{
  
  ourhelper = new DbHelper(ourcontext);
  ourdatabase = ourhelper.getWritableDatabase();
  return this;
   }
   public void close(){
  ourhelper.close();
  }
  // method that i want to receive in BroadcastReciever 
  public String Spclgetno(String spno)  throws SQLException{
  // TODO Auto-generated method stub
  
  String[] columns = new String[] {ROWID_PRF, PRF_NO, PRF_NAME };
  //ourdatabase = ourhelper.getWritableDatabase();
  Cursor c = ourdatabase.query(TABLE_PROFILE, columns,  PRF_NO + "='" + spno + "'",  
      null, null, null, null);&lt;------- also in this line query show exception
  
  if(c != null &amp;&amp; c.moveToFirst() ) {     //&amp;&amp; c.getCount() &gt; 0
  
  
  
      String retsno = c.getString(1);
      Log.i("prfno", retsno.toString());
      return retsno;
  }
  return null;
    }
  </code></pre>
  
  <p>here is my loggate </p>
  
  <pre><code> 04-10 15:06:14.923: W/dalvikvm(29010): threadid=1: thread exiting with uncaught   
   exception (group=0x41f852a0)
   04-10 15:06:14.943: E/AndroidRuntime(29010): FATAL EXCEPTION: main
   04-10 15:06:14.943: E/AndroidRuntime(29010): java.lang.RuntimeException: Unable to   
   start receiver com.smslocker.SmsReciever: java.lang.NullPointerException
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
   android.app.ActivityThread.handleReceiver(ActivityThread.java:2269)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
   android.app.ActivityThread.access$1600(ActivityThread.java:134)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at    
   android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at    
   android.os.Handler.dispatchMessage(Handler.java:99)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at     
   android.os.Looper.loop(Looper.java:137)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
   android.app.ActivityThread.main(ActivityThread.java:4867)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
   java.lang.reflect.Method.invokeNative(Native Method)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
   java.lang.reflect.Method.invoke(Method.java:511)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at  
   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
   dalvik.system.NativeStart.main(Native Method)
   04-10 15:06:14.943: E/AndroidRuntime(29010): Caused by: java.lang.NullPointerException
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at    
   com.smslocker.SmslockDB.Spclgetno(SmslockDB.java:109)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
   com.smslocker.SmsReciever.onReceive(SmsReciever.java:51)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   at    
   android.app.ActivityThread.handleReceiver(ActivityThread.java:2262)
   04-10 15:06:14.943: E/AndroidRuntime(29010):   ... 10 more
   04-10 15:06:15.003: W/CursorWrapperInner(29010): Cursor finalized without prior close()
   04-10 15:06:15.003: D/dalvikvm(29010): GC_CONCURRENT freed 311K, 13% free   
   10230K/11719K, paused 12ms+5ms, total 111ms
  </code></pre>
  
  <p>i regester in example like that </p>
  
  <pre><code>   &lt;receiver
          android:name="com.smslocker.SmsReciever"
          android:exported="true"
          android:enabled="true"&gt;
          &lt;intent-filter android:priority="999" &gt;
              &lt;action android:name="android.provider.Telephony.SMS_RECEIVED" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  </code></pre>