Tuesday, June 21, 2011

How to fix Activity.performStop NullPointer Exception

Just now I get a strange NullPointerException when I want to open my preferences activity in my app.

Logcat says:
E/AndroidRuntime(11226): FATAL EXCEPTION: main
E/AndroidRuntime(11226): java.lang.RuntimeException: Unable to stop activity {com.jjoe64.tests/com.jjoe64.tests.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(11226):  at
E/AndroidRuntime(11226):  at
E/AndroidRuntime(11226):  at$1800(
E/AndroidRuntime(11226):  at$H.handleMessage(
E/AndroidRuntime(11226):  at android.os.Handler.dispatchMessage(
E/AndroidRuntime(11226):  at android.os.Looper.loop(
E/AndroidRuntime(11226):  at
E/AndroidRuntime(11226):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(11226):  at java.lang.reflect.Method.invoke(
E/AndroidRuntime(11226):  at$
E/AndroidRuntime(11226):  at
E/AndroidRuntime(11226):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(11226): Caused by: java.lang.NullPointerException
E/AndroidRuntime(11226):  at
E/AndroidRuntime(11226):  at
E/AndroidRuntime(11226):  ... 11 more

There's no method called, which was written by me. So, is it an Android bug?
The best idea now is to take a look in the source code of Android:
final void performStop() {
 synchronized (mManagedCursors) {
  final int N = mManagedCursors.size();
  for (int i=0; i<N; i++) {
   ManagedCursor mc = mManagedCursors.get(i);
   if (!mc.mReleased) {
   mc.mCursor.deactivate(); // -------> NullPointer
   mc.mReleased = true;

It's clear, that the problem is that a cursor is "null".
So the reason of that issue was found fast: Never return "null" in your content provider's query method.
Instead of null, you should throw an exception or return an empty MatrixCursor like that:
return new MatrixCursor(new String[] {"_id"}); // never return null

A really nice way to detect, null-cursor managing, is to override the startManagingCursor method:
 public void startManagingCursor(Cursor c) {
  if (c == null) {
   throw new IllegalStateException("cannot manage cursor: cursor == null");


  1. Apsolutelly brilliant... You helped me a lot...
    Thank you...

    Conclusion: NEVER RETURN NULL!!! :)

  2. thank you very much it helped me a lot !!!