web-dev-qa-db-fra.com

NullPointerException lors de l'ajout d'un objet à ArrayList dans Android

J'essaie de créer une vue de liste avec BaseAdapter et je continue d'obtenir une erreur de référence d'objet nul au moment d'ajouter un objet à une liste de tableaux.

Le code pertinent est ci-dessous:

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]));
        }
    }

    ...
}

J'obtiens l'erreur sur la ligne ci-dessous (découverte lors du débogage):

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

Voici l'erreur réelle dans 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)

Je ne comprends pas comment j'obtiens un objet nul lorsque je crée le nouvel objet Creator à ajouter à ArrayList.

Tout pointeur serait grandement apprécié.

11
rapidclock

Vous n'initialisez jamais ArrayList<Creator> list;. Donc list.add(new Creator(title[i], descs[i], icons[i])); lancera un NPE.

Changement:

ArrayList<Creator> list;

à:

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

list est nul car vous ne l'avez pas initialisé.

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