0

I get a crash report from my production app sometimes, the line below I think is the source of the problem:

Attempt to invoke virtual method 'java.util.ArrayList foto.studio.SQLiteDataSource.getAllImageItems()' on a null object reference at foto.studio.MainActivity.onCreateOptionsMenu(MainActivity.java:267)

the method it is pointing to inside MainActivity (and the responsible line highlighted with "==>" ):

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        final Menu m = menu;
        final MenuItem item = menu.findItem(R.id.action_notifications);
        View actionView = item.getActionView();
        notifTextView = (TextView) actionView.findViewById(R.id.notifTextView);

        ArrayList <ImageItemsSetter> pendingOrdersList = new ArrayList<ImageItemsSetter>();

        ===> pendingOrdersList = datasource.getAllImageItems(); <===

        Log.i("value", "String calue of pendingorderlist: " + String.valueOf(pendingOrdersList.size()));
        String unreadNotifs = String.valueOf(pendingOrdersList.size());

        if (!unreadNotifs.equals("0")) {
            notifTextView.setText(unreadNotifs);
        } else {
            notifTextView.setVisibility(View.GONE);
        }       

        item.getActionView().setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                m.performIdentifierAction(item.getItemId(), 0);
            }
        });


        return true;
    }

And the method from SQLiteDataSource:

public ArrayList<ImageItemsSetter> getAllImageItems() {
        Cursor cursor = database.query(SQLiteHelper.TABLE_IMAGES,
                allColumnsImages, null, null, "product", null, null);

        //Log.i("LOGTAG", "Returned " + cursor.getCount() + " rows");
        ArrayList<ImageItemsSetter> products = cursorToListImages(cursor);
        return products;
    }

What could be wrong? My ArrayList (pendingOrdersList) is initiated right before I use it, why does it say that it is null??

Line where I initialize the database:

@Override
    protected void onResume() {
        super.onResume();
        initImageLoader();

        datasource = new SQLiteDataSource(this);
        datasource.open();
        invalidateOptionsMenu();

        // facebook analytics
        AppEventsLogger.activateApp(this);

        //get or update appVersion string
        getAppVersion();

    }

Full Report:

java.lang.RuntimeException: Unable to start activity ComponentInfo{foto.studio/foto.studio.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList foto.studio.SQLiteDataSource.getAllImageItems()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList foto.studio.SQLiteDataSource.getAllImageItems()' on a null object reference at foto.studio.MainActivity.onCreateOptionsMenu(MainActivity.java:267) at android.app.Activity.onCreatePanelMenu(Activity.java:2947) at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:606) at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:980) at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:308) at com.android.internal.policy.impl.PhoneWindow.doPendingInvalidatePanelMenu(PhoneWindow.java:954) at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2194) at android.app.Activity.onRestoreInstanceState(Activity.java:1092) at android.app.Activity.performRestoreInstanceState(Activity.java:1037) at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1175) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666) ... 10 more

Edmond Tamas
  • 3,148
  • 9
  • 44
  • 89
  • 4
    [Check this out](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it).. Obviously your `datasource` is null. – Codebender Jul 10 '15 at 14:36
  • @Codebender, ok, the query maybe null, but I only assign it to "pendingOrdersList", I do no operations there. And bellow I only check if it returns null to set some text in a textview which has nothing to do with the line the ecrash report points to. – Edmond Tamas Jul 10 '15 at 14:42
  • @Codebender, oh you maybe right. Sorry. But I initialise datasource onResume(). Please see my updated question. – Edmond Tamas Jul 10 '15 at 14:44
  • Edmond, but wont onResume() only be called on application resuming... I guess it wont be called when the application starts fresh... And secondly, android will never lie to you about an object being null when it's not... So you can be very sure it's null... – Codebender Jul 10 '15 at 14:48
  • @Codebender should I initialise my database both in onCreate() and onResume(()? – Edmond Tamas Jul 10 '15 at 14:50

1 Answers1

1

Be careful when you use custom code in onCreateOptionsMenu() : this method may be called at any time after onCreate(), depending on the Android version and other factors. Your data source is obviously not yet ready when the method is called, so you must handle that case too.

I may suggest that you test if the data source is null before initializing the menu items, and as soon as your data source is ready, initialize the items if it was not yet done.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51