Reputation: 2606
I am trying to populate my UI with data from DB with Cursor loader.
Below is on my onCreateLoader()
@Override
public Loader < Cursor > onCreateLoader(int id, Bundle args) {
String sortingOrder = MovieContract.Movie.COLUMN_RATING + " ASC";
Uri uri = MovieContract.Movie.buildMovieDbUri();
Log.v(LOG_TAG, "URI: " + uri.toString() + " sorting: " + sortingOrder);
Cursor c = getActivity().getContentResolver().query(uri, MOVIE_PROJECTION, null, null, sortingOrder);
if (c.moveToFirst())
Log.v(LOG_TAG, "Movie ID: " + c.getInt(PROJ_MOVIE_ID - 1));
else
Log.v(LOG_TAG, "Fail");
CursorLoader cc = new CursorLoader(getActivity(),
uri,
MOVIE_PROJECTION,
null,
null,
sortingOrder);
return cc;
}
@Override
public void onLoadFinished(Loader < Cursor > loader, Cursor data) {
mAdapter.swapCursor(data);
}
Exception
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor itsme.com.moviecatalogue.GridViewAdapter.swapCursor(android.database.Cursor)' on a null object reference
In the above code cursor c gets me all the data correctly. But I get a NullPointerException
on swapCursor()
in onLoadFinished()
. I dont understand what am I doing wrong.
Upvotes: 0
Views: 498
Reputation: 21
To set mAdapter
Properly add following code before using mAdapter
mAdapter = new <nameOfCursorAdapter>(this,null);
for Example if your Cursor adapter name is myCustomCursorAdapter
then code will like this
mAdapter = new myCustomCursorAdapter(this,null);
Upvotes: 1
Reputation: 42460
You are trying to call method swapCursor()
on mAdapter
, which equals null at this point. This results in the NullPointerException you are seeing.
To resolve this, make sure that mAdapter
is set properly before the method call.
Upvotes: 2