Null Pointer Exception in my BroadcastReceiver class

Posted by user1760007 on Stack Overflow See other posts from Stack Overflow or by user1760007
Published on 2012-10-19T16:52:33Z Indexed on 2012/10/19 17:00 UTC
Read the original article Hit count: 218

Filed under:
|
|

I want to search a db and toast a specific column on the startup of the phone. The app keeps crashing and getting an exception even though I feel as the code is correct.

@Override
    public void onReceive(Context ctx, Intent intent) {
        Log.d("omg", "1");
        DBAdapter do = new DBAdapter(ctx);
        Log.d("omg", "2");
        Cursor cursor = do.fetchAllItems();
        Log.d("omg", "3");
        if (cursor.moveToFirst()) {
            Log.d("omg", "4");
            do {
                Log.d("omg", "5");
                String title = cursor.getString(cursor.getColumnIndex("item"));
                Log.d("omg", "6");
                // i = cursor.getInt(cursor.getColumnIndex("id"));
                Toast.makeText(ctx, title, Toast.LENGTH_LONG).show();

            } while (cursor.moveToNext());
        }
        cursor.close();

    }

The frustrating part is that I don't see any of my "omg" logs show up in logcat. I only see when my application crashes.

I get three lines of errors in logcat.

10-19 12:31:11.656: E/AndroidRuntime(1471): java.lang.RuntimeException: Unable to start receiver com.test.toaster.MyReciever: java.lang.NullPointerException

10-19 12:31:11.656: E/AndroidRuntime(1471):     at com.test.toaster.DBAdapter.fetchAllItems(DBAdapter.java:96)

10-19 12:31:11.656: E/AndroidRuntime(1471):     at com.test.toaster.MyReciever.onReceive(MyReciever.java:26)

For anyone interested, here is my DBAdapter fetchAllItems code:

public Cursor fetchAllItems() {
        return mDb.query(DATABASE_TABLE, new String[] { KEY_ITEM, KEY_PRIORITY,
            KEY_ROWID }, null, null, null, null, null);
    }

© Stack Overflow or respective owner

Related posts about java

Related posts about android