Getting Null Pointer Exception after Listing 9.14


#1

After updating the code from listing 9.14 (Setting up the ArrayAdapter (CrimeListFragment.java) I’m getting the following null pointer exception from my logcat output. I should be getting some list items but the app is just crashing at this point. Any help would be much appreciated. Logcat, CrimelistFragment.java, CrimeLab.java, and Crime.java are below. Thanks in advance.

Logcat:

2-03 19:28:22.948 3838-3838/criminalintent.android.bignerdranch.com.criminalintent E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: criminalintent.android.bignerdranch.com.criminalintent, PID: 3838 java.lang.RuntimeException: Unable to start activity ComponentInfo{criminalintent.android.bignerdranch.com.criminalintent/criminalintent.android.bignerdranch.com.criminalintent.CrimeListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList criminalintent.android.bignerdranch.com.criminalintent.CrimeLab.getCrimes()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) 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:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList criminalintent.android.bignerdranch.com.criminalintent.CrimeLab.getCrimes()' on a null object reference at criminalintent.android.bignerdranch.com.criminalintent.CrimeListFragment.onCreate(CrimeListFragment.java:19) at android.support.v4.app.Fragment.performCreate(Fragment.java:1763) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220) at android.app.Activity.performStart(Activity.java:5949) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) 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:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

CrimeListFragment.java

[code]package criminalintent.android.bignerdranch.com.criminalintent;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;

import java.util.ArrayList;

/**

  • Created by lewie on 12/1/14.
    */
    public class CrimeListFragment extends ListFragment {
    private ArrayList mCrimes;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().setTitle(R.string.crimes_title);
    mCrimes = CrimeLab.get(getActivity()).getCrimes();

     ArrayAdapter<Crime> adapter = new ArrayAdapter<Crime>(getActivity(),android.R.layout.simple_list_item_1, mCrimes);
     setListAdapter(adapter);
    

    }
    }

[/code]

CrimeLab.java

package criminalintent.android.bignerdranch.com.criminalintent;

import android.content.Context;

import java.util.ArrayList;
import java.util.UUID;

/**
 * Created by Bahamut on 12/1/14.
 */
public class CrimeLab {
    private ArrayList<Crime> mCrimes;

    public static CrimeLab sCrimeLab;
    private Context mAppContext;

    private CrimeLab(Context appContext) {
        mAppContext = appContext;

        mCrimes = new ArrayList<Crime>();
        for (int i = 0; i < 100; i++) {
            Crime c = new Crime();
            c.setTitle("Crime #" + i);
            c.setmSolved(i % 2 == 0); // Every other one
            mCrimes.add(c);
        }
    }

    public static CrimeLab get(Context c) {
        if (sCrimeLab != null) {
            sCrimeLab = new CrimeLab(c.getApplicationContext());
        }
        return sCrimeLab;
    }

    public ArrayList<Crime> getCrimes() {
        return mCrimes;
    }

    public Crime getCrime(UUID id) {
        for (Crime c : mCrimes) {
            if (c.getID().equals(id)) //make sure this is correct getID method
                return c;
        }
        return null;
    }
}

Crime.java

package criminalintent.android.bignerdranch.com.criminalintent;

import java.util.Date;
import java.util.UUID;

/**
 * Created by lewie on 11/30/14.
 */
public class Crime {

    private UUID mID;
    private String mTitle;
    private Date mDate;
    private boolean mSolved;

    public Date getmDate() {
        return mDate;
    }

    public void setmDate(Date mDate) {
        this.mDate = mDate;
    }

    public boolean ismSolved() {
        return mSolved;
    }

    public void setmSolved(boolean mSolved) {
        this.mSolved = mSolved;
    }

    public Crime() {
        //generate unique identifier
        mID = UUID.randomUUID();
        mDate = new Date();
    }

    public UUID getID() {
        return mID;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }


}

#2

Nevermind :laughing: Found and corrected the issue. Fix below -

CrimeLab.java

public static CrimeLab get(Context c) {
        if (sCrimeLab != null) {     // ***THIS SHOULD HAVE BEEN  if (sCrimeLab == null)***  
            sCrimeLab = new CrimeLab(c.getApplicationContext());
        }
        return sCrimeLab;
    }