NullPointerException on back button with no crimes


Hello guys.

I got a NullPointerException after I removed all the crimes. Here is what I do:

  1. Given there is not crimes created.
  2. Create a new crime (the information is irrelevant)
  3. View your crime on the list of crimes
  4. Enter to see the detail of the crime created
  5. Delete that crime
  6. List of crimes is now empty
  7. Press the back button on the cellphone and you should get a NullPointerException because the system is referring to mCrime in onCreateView() for the file

The problem is mCrime is null so all the instance variables get in trouble. The solution I proposed is added at the end:

    public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_crime, parent, false);
        // Setting the up button
        	if (NavUtils.getParentActivityName(getActivity()) != null) {
        // Nullable Crime instance in case I removed all the
        // crimes and want to avoid a nullPointerException
        if (mCrime == null) {
        	mCrime = new Crime();

I don’t know if this was asking before or if this is coverage by the book. But I consider important to mention at least.

Btw: I love the book :wink:


Gabriel Muñumel