0

I'm getting a NullPointerException right where I call the query from my Fragment class. I'm fairly certain the issue is because I'm instantiating my ContentProvider class in my Fragment class, but I don't know how else I can call the ContentResolver if I don't have the context. I'm pretty new to Android programming and it's hard for me to get a grasp on things at first so forgive me if it's a really obvious error.

So here is part of the code,

abstract private class BaseTask<T> extends AsyncTask<T, Void, Cursor> {
@Override
public void onPostExecute(Cursor result) {
  ((CursorAdapter)getListAdapter()).changeCursor(result);
 }

     protected Cursor doQuery() { // This method sets up my query call in ConstantsFragment class.
     Uri uri = ConstantsProvider.Constants.CONTENT_URI;
     String[] projection = new String[] { ConstantsProvider.Constants.TITLE,
             ConstantsProvider.Constants.VALUE };
     String selection = ConstantsProvider.Constants.TABLE + " = '"
           + ("1") + "'";
     String[] selectionArgs = null;
     String sortOrder = ConstantsProvider.Constants.DEFAULT_SORT_ORDER
           + " COLLATE LOCALIZED ASC";

     Cursor result = cp.getContext().getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder); 

    //Above is where I get my nullpointer. cp is my Contentprovider class object.

     return result;
     }
}

I'm leaving my ContentProvider and ConstantsFragment class(which is the class that includes the above code) below incase needed. In my ConstantsFragment class I have no completed the insert/delete methods so ignore those for now, I want to make sure my query works first.

ContentProvider class : http://pastebin.com/NadkFGBA

ConstantsFragment class: http://pastebin.com/tGERRjFF

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Arian.No
  • 3
  • 1
  • 5
  • 1
    Where is `cp` declared and initialized? – Jens Jun 24 '15 at 05:18
  • Sorry I should've formatted it better, cp is initialized in the very top of my constantsfragment class. I put that at the bottom of my post in a pastebin link if you want to take a look. – Arian.No Jun 24 '15 at 05:21

0 Answers0