NullPointerException when adding an object to ArrayList in Android

rapidclock picture rapidclock · Feb 9, 2015 · Viewed 63.8k times · Source

I'm trying to create a List View with BaseAdapter and i keep getting a Null Object Reference Error at the point of adding an object to an ArrayList.

Relevant code is below:

public class MainActivity extends ActionBarActivity {
    int[] icons = {R.drawable.icon01, R.drawable.icon02, R.drawable.icon03, R.drawable.icon04, R.drawable.icon05, R.drawable.icon06, R.drawable.icon07, R.drawable.icon08, R.drawable.icon09, R.drawable.icon10, R.drawable.icon11, R.drawable.icon12};
    ListView listView;
    String[] title, desc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);

        Resources resources = getResources();
        title = resources.getStringArray(R.array.titles);
        desc = resources.getStringArray(R.array.desc);

        RT_Adapter rt_adapter = new RT_Adapter(this, title, desc, icons);
        listView.setAdapter(rt_adapter);
    }
}

class Creator {
    String a, b;
    int c;

    Creator(String d, String e, int f) {
        this.a = d;
        this.b = e;
        this.c = f;
        Log.d("RT", a +" - "+b+" - "+c); //Position 01 - This is Description 01 - 2130837556
    }
}

class RT_Adapter extends BaseAdapter {
    Context c;
    ArrayList<Creator> list;

    public RT_Adapter(Context context, String[] title, String[] descs, int[] icons) {
        this.c = context;
        for (int i = 0; i < title.length; i++) {
            list.add(new Creator(title[i], descs[i], icons[i]));
        }
    }

    ...
}

I get the error at the below line(found out while debugging):

list.add(new Creator(title[i], descs[i], icons[i]));

Below is the actual Error in Logcat:

02-09 17:09:05.494      767-767/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.rt.droid.baseadapter01, PID: 767
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rt.droid.baseadapter01/com.rt.droid.baseadapter01.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' 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:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
            at com.rt.droid.baseadapter01.RT_Adapter.<init>(MainActivity.java:71)
            at com.rt.droid.baseadapter01.MainActivity.onCreate(MainActivity.java:36)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            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:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

I don't Understand how i am getting a null object when i create the new Creator object to add to the ArrayList.

Any pointers would be greatly appreciated.

Answer

Jens picture Jens · Feb 9, 2015

You never initialize ArrayList<Creator> list;. So list.add(new Creator(title[i], descs[i], icons[i])); will throw a NPE.

Change:

ArrayList<Creator> list;

to:

ArrayList<Creator> list = new ArrayList<Creator>();