1

我不明白我的代码有什么问题。我在 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());
4

2 回答 2

2

将您的声明更改为

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

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

于 2013-01-08T18:53:27.113 回答
0

您应该在使用它之前进行实例myAddressUniquenessmyAddressUniqueness = new ArrayList<String>()

于 2013-01-08T18:51:20.867 回答