Reputation: 3305
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
Upvotes: 0
Views: 3966
Reputation: 12929
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.
Upvotes: 1