NullPointer在Android中向ArrayList添加对象时的异常

我正在尝试使用BaseAdapter创建一个列表视图,并且在将对象添加到ArrayList时,我不断收到一个空对象引用错误。

相关代码如下:

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

    ...
}

我在以下行收到错误(调试时发现):

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

以下是 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)

我不明白当我创建新的Creator对象以添加到ArrayList时,我如何获得空对象。

任何指针将不胜感激。


答案 1

您从不初始化 .所以会抛出一个NPE。ArrayList<Creator> list;list.add(new Creator(title[i], descs[i], icons[i]));

改变:

ArrayList<Creator> list;

自:

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

答案 2

list为 null,因为您尚未初始化它。

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

推荐