Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList空指针异常_Java_Android_Listview_Arraylist_Nullpointerexception - Fatal编程技术网

Java ArrayList空指针异常

Java ArrayList空指针异常,java,android,listview,arraylist,nullpointerexception,Java,Android,Listview,Arraylist,Nullpointerexception,我已经创建了一个arraylist和一个ListView。我打算遍历ListView,检查它们是否被选中,然后将对象(使用ListView.getItem())添加到我的arraylist中。但是我得到了一个NullPointerException。选定的ArrayList people_在类的顶部声明如下: ArrayList<PeopleDetails> selected_people; ArrayList选中的\u人; 我的代码是: for (int i = 0; i &l

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

ArrayList<PeopleDetails> selected_people;
ArrayList选中的\u人;
我的代码是:

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(int i=0;i
我在线路上出错了

for(人员详细信息人员:所选人员)

说“NullPointerException” . 我认为这意味着arraylist为null,无法理解为什么它应该为null。我是不是在班上名列前茅的时候说错话了?还是我的选择和添加方法有问题?

您错过了

people_selected = new ArrayList<PeopleDetails>();
people_selected = new ArrayList<PeopleDetails>();
people\u selected=new ArrayList();
您已声明但未初始化。

您错过了

people_selected = new ArrayList<PeopleDetails>();
people_selected = new ArrayList<PeopleDetails>();
people\u selected=new ArrayList();

您已声明但未初始化。

代码显示您已声明变量,但未显示您已初始化变量,如下所示:

ArrayList<PeopleDetails> selected_people;
people\u selected=new ArrayList();

代码显示您声明了变量,但没有显示您初始化了变量,如下所示:

ArrayList<PeopleDetails> selected_people;
people\u selected=new ArrayList();

您声明了所选人员,但您正在使用所选人员

从来没有填写过的…

您声明了选定的人,但您使用的是选定的人

它从不填充…

增强型for循环大致就是这种结构

for (Iterator<T> i = someList.iterator(); i.hasNext();) {

}
您会注意到,增强型for循环不会抛出任何
NullPointerException
,因为
someList.iterator()
现在返回一个迭代器,
i.hasNext()
返回
false
,这样循环就不会继续


PS:增强型for循环骨架取自。

增强型for循环大致就是这种结构

for (Iterator<T> i = someList.iterator(); i.hasNext();) {

}
您会注意到,增强型for循环不会抛出任何
NullPointerException
,因为
someList.iterator()
现在返回一个迭代器,
i.hasNext()
返回
false
,这样循环就不会继续


PS:增强的for循环骨架取自。

发生错误的原因是您尚未初始化数组

加上这个解决了我的问题

selected_people = new ArrayList<PeopleDetails>();
selected_people=new ArrayList();

发生错误是因为您尚未初始化数组

加上这个解决了我的问题

selected_people = new ArrayList<PeopleDetails>();
selected_people=new ArrayList();

Add“ArrayList people\u selected=new ArrayList();在Declatement和添加到item part时,您对arraylist使用了不同的变量名?向对象声明引用变量只会为您在将来某个时候将其设置为真实对象的引用保留空间。在将其设置为引用之前,它将指向nothing或null,这会导致空指针当您尝试取消引用它时(纯粹主义者,我知道统一化的null引用指向null对象,但为了简单的注释……)添加“ArrayList people_selected=new ArrayList();在Declatement和Add to item part中为arraylist使用不同的变量名?向对象声明引用变量只会为您保留空间,以便在将来某个时候将其设置为真实对象的引用。在您将其设置为引用之前,它指向nothing或null,这会在您尝试取消引用它时导致null指针异常(纯粹主义者,我知道统一化的null引用指向null对象,但为了简单的注释…),这只是问题中的一个输入错误。。。谢谢你!我已经改正了。:)这只是问题中的一个输入错误。。。谢谢你!我已经改正了。:)