初始化arraylist的值

时间:2013-01-08 18:49:16

标签: android database arraylist

我无法理解我的代码中出了什么问题。我在myAddressUniqueness中收到错误。不知道为什么。在我尝试使用字符串数据类型之前,却得到了同样的错误。它是说java.null异常。

ArrayList<String>  myAddressUniqueness = null;
    String name = "hello";

    if (indexBody < 0 || !cursor.moveToFirst())
        return;

    smsList.clear();

    do {
        // int cursorPostion = cursor.getPosition();

        String address;
        String msgStr = cursor.getString(indexBody);
        String senderNumber = cursor.getString(indexAddr);
        Log.d("Name : ", senderNumber);
        // String name = cursor.getString(cursor
        // .getColumnIndex(PhoneLookup.DISPLAY_NAME));
        // Log.d("Name : ",name);

        if (name != null) {
            address = name;
        } else {
            address = senderNumber;
        }
        Log.d("Address: ", address);
        flag = 1;
        //Log.e("Number: ", addressUniqueness.length + "");

        for (j = 0; j < myAddressUniqueness.size(); j++) {
            if (myAddressUniqueness.contains(address)) {
                flag = 0;
                break;
            }

        }

        if (flag == 1) {
            myAddressUniqueness.add(new String(address));
            i++;

            String str = "Sender: " + address + "\n";
            smsList.add(str);

        }

        // TODO Auto-generated catch block

    } while (cursor.moveToNext());

2 个答案:

答案 0 :(得分:2)

将您的声明更改为

ArrayList<String> myAddressUniqueness = new ArrayList<String>();

您已将其初始化为null。这将实例化它,然后您可以向其添加数据。

答案 1 :(得分:0)

在使用之前,您应该myAddressUniqueness myAddressUniqueness = new ArrayList<String>()实例化。