Val Okafor Val Okafor - 5 months ago 24
Android Question

Unexplainable Null Pointer Exception in Android

I am getting a

NullPointerException
in the following method and I have tried everything I can think off without success.

private void showCustomListView(int year, int month, int day) {

List<Transaction> mTransactionList = new ArrayList<Transaction>();
mTransactionList = AppManager.getTransactionsByDay(year, month, day);
DailyTransactionAdapter mAdapter = new DailyTransactionAdapter(getActivity(), 0, mTransactionList);
dailyTransactionListView.setAdapter(mAdapter);
}


Here is the context: I have an Activity :
Activity_A
hosting
Fragment_A
. The above method is called from the
onResume()
callback of Fragment_A and here is that o
nResume()
method

@Override
public void onResume() {
super.onResume();
//When the Fragment resumes set the shown date to today
mYear = selectedDate.get(Calendar.YEAR);
mMonth = selectedDate.get(Calendar.MONTH);
mDay = selectedDate.get(Calendar.DAY_OF_MONTH);
tvDisplayDate.setText(new StringBuilder()
.append(mMonth + 1).append("-").append(mDay).append("-")
.append(mYear).append(" "));

showCustomListView(mYear, mMonth, mDay);
btnChangeDate.setOnClickListener(new View.OnClickListener() {
@Override
//After the view is created set the onclick lister for the
//Change date button
public void onClick(View view) {
showDatePicker();
}
});

}


The data for this adapter comes from an in memory list like so. This is the code section from the AppManager

public static List<Transaction> getTransactionsByDay(int year, int month, int day){
List<Transaction> dayTransactions = new ArrayList<Transaction>();
List<Transaction> allTransactions = getTransactionList();
Log.i(TAG, "Size: " + allTransactions.size());
for (Transaction tempTransaction : allTransactions){
if (tempTransaction.getTransactionDate().get(Calendar.YEAR) == year &&
tempTransaction.getTransactionDate().get(Calendar.MONTH) == month &&
tempTransaction.getTransactionDate().get(Calendar.DAY_OF_MONTH) == day)
{
dayTransactions.add(tempTransaction);
}

}
return dayTransactions;
}


This works fine as expected when I start the app as in run the app from the IDE (Android Studio) to the device. The problem comes when I start another Activity - Activity_B, after the above list running and visible, and in Activity_B I add an item to the in memory list and the size of the list goes say from 10 to 11.

When I now go from Activity_B to Fragment_A the app crashes with an NPE at the
showCustomListView()
method above. I have tried to clear the list, I have tried
adapter.notifydatasetchanged()
all without success, it keeps crashing at that method only after I add to the list while the app is running, it will run fine after I restart the app.

What else should I try: Here some of the NPE

11-04 09:36:19.147 6464-6464/com.valuecardnfc.valuecardnfc E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.valuecardnfc.valuecardnfc, PID: 6464
java.lang.NullPointerException
at com.valuecardnfc.valuecardnfc.Models.AppManager.getTransactionsByDay(AppManager.java:80)
at com.valuecardnfc.valuecardnfc.Fragments.DailyTransactionFragment.showCustomListView(DailyTransactionFragment.java:160)
at com.valuecardnfc.valuecardnfc.Fragments.DailyTransactionFragment.onResume(DailyTransactionFragment.java:102)
at android.support.v4.app.Fragment.performResume(Fragment.java:1521)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:823)
at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1128)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1477)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)

Answer
tempTransaction.getTransactionDate().get(Calendar.YEAR)

This has two possibilities for a NullPointerException:

  1. tempTransaction is null.

  2. getTransactionDate() returns null.

Since you use the return value of getTransactionDate() in multiple locations, you should create a new variable:

Calendar transactionDate = tempTransaction.getTransactionDate();

Now you can check if this transactionDate is null and you can reuse it in your if condition. You will also immediately know if tempTransaction is null because this line will throw a NullPointerException in such a case.