ArrayList空指针异常

 勋酥-osh海州吴氏 发布于 2023-01-31 14:06

我创建了一个arraylist和一个ListView.我打算遍历ListView,检查它们是否被检查,然后将对象(使用ListView.getItem())添加到我的arraylist.但是我得到一个NullPointerException.ArrayList people_selected,在类顶部声明如下:

ArrayList selected_people;

我的代码:

for (int i = 0; i < people_list.getCount(); i++) {
              item_view = people_list.getAdapter().getView(i, null, null);
                chBox = (CheckBox) item_view.findViewById(R.id.checkBox);//your xml id value for checkBox.
                if (chBox.isChecked()) {
                    selected_people.add((PeopleDetails) people_list.getItemAtPosition(i));
                }
            }

        for(PeopleDetails person : selected_people){

            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(person.number, null, sms_message, null, null);        
            }
        ///and now need to write party to file.

我在线上得到一个错误

for(PeopleDetails person:selected_people)

说"NullPointerException".我认为这意味着arraylist是null,并且无法弄清楚为什么它应该为null.我是否在课堂上声明错了?或者我的选择和添加方法有问题?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有