androidnullpointerexceptionandroid-cursorandroid-loadermanager

NullPointerException in restartLoader Method of LoaderManager


I have the following own Interface Implementation in my Fragment:

@Override
    public void onReportChanged(Fragment sender, long id, int position) {
        // Views ein und ausblenden
        _List.setVisibility(View.GONE);
        _OnLoading.setVisibility(View.VISIBLE);
        _NoDataView.setVisibility(View.GONE);
        _ReportId = id;
        getLoaderManager().restartLoader(_LM_REPORTS, null, this);
    };

The FragmentActivity report to this Fragment, that another Fragment (Selection List) have selected one item. After that the ListFragment should load new Data with reported ID (_ReportId).

But I get NullPoinerException on this code before going to onCreateLoader-Method. Here is the LogCat:

05-28 14:24:37.905: E/AndroidRuntime(1775): FATAL EXCEPTION: main
05-28 14:24:37.905: E/AndroidRuntime(1775): java.lang.NullPointerException
05-28 14:24:37.905: E/AndroidRuntime(1775):     at android.support.v4.app.LoaderManagerImpl.restartLoader(LoaderManager.java:637)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at de.webducer.android.worktime.beta.ui.fragment.ReportListFragment.onReportChanged(ReportListFra gment.java:142)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at de.webducer.android.worktime.beta.ui.ReportListActivity.onReportSelected(ReportListActivity.java:97)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at de.webducer.android.worktime.beta.ui.fragment.ReportSelectorSpinnerFragment$1.onItemSelected(ReportSelectorSpinnerFragment.java:78)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:882)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at android.widget.AdapterView.access$200(AdapterView.java:48)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:848)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at android.os.Handler.handleCallback(Handler.java:605)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at android.os.Looper.loop(Looper.java:137)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at java.lang.reflect.Method.invoke(Method.java:511)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-28 14:24:37.905: E/AndroidRuntime(1775):     at dalvik.system.NativeStart.main(Native Method)

I don'T understand why. The same code (but for the other list) works fine on the other FragmentActivity.


Solution

  • I found the reason:

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle extra) {
        CursorLoader returnValue = null;
        if (_ReportId > 0) {
            switch (id) {
                case _LM_REPORTS:
                    returnValue = new CursorLoader(getActivity(),
                        ReportContentProvider.CONTENT_URI_REPORT_DATA,
                        null,
                        ReportTable.COLUMN_ID + " =?",
                        new String[] { String.valueOf(_ReportId) },
                        null);
                        break;
    
                    default:
                        break;
                }
            }
        return returnValue;
    }
    

    The Loader have allways to run. The condition if (_ReportId > 1) produced the exception. Without the condition the code runs fine.