关于java:在Android中向ArrayList添加对象时出现NullPointerException

NullPointerException when adding an object to ArrayList in Android

本问题已经有最佳答案,请猛点这里访问。

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

相关代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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]));
        }
    }

    ...
}

我在下面的行中得到错误(在调试时发现):

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

以下是Logcat中的实际错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at android.app.ActivityThread.access$800(ActivityThread.java:144)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at android.os.Handler.dispatchMessage(Handler.java:102)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at android.os.Looper.loop(Looper.java:135)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at android.app.ActivityThread.main(ActivityThread.java:5221)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.lang.reflect.Method.invoke(Native Method)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at java.lang.reflect.Method.invoke(Method.java:372)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

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

任何指针都将非常感激。


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

更改:

1
ArrayList<Creator> list;

至:

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

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

1
2
3
4
5
6
7
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]));
                }
            }