Java ArrayList 空指针异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21349733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArrayList null pointer exception
提问by Swedish Architect
I have created an arraylist, and a ListView. I intend to iterate through the ListView, and checking whether they are checked or not, and then add the object (using ListView.getItem()) to my arraylist. however I get a NullPointerException. The ArrayList people_selected, is declared at top of class like this:
我创建了一个数组列表和一个 ListView。我打算遍历 ListView,并检查它们是否被选中,然后将对象(使用 ListView.getItem())添加到我的数组列表中。但是我得到一个 NullPointerException。ArrayList people_selected,在类的顶部声明如下:
ArrayList<PeopleDetails> selected_people;
And my code:
还有我的代码:
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.
I get an error at the line
我在线路上遇到错误
for(PeopleDetails person : selected_people)
for(人物详细信息人:selected_people)
Saying "NullPointerException" . I take this to mean that the arraylist is null, and cannot figure out why it should be null. Have I declared it wrong at the top of my class? Or is my selection and add method faulty?
说“NullPointerException”。我认为这意味着 arraylist 为空,并且无法弄清楚为什么它应该为空。我在班级最高处宣布错误了吗?还是我的选择和添加方法有问题?
采纳答案by Suresh Atta
ArrayList<PeopleDetails> people_selected;
You declared and never initialized. Just initialize it before using it. Otherwise NullPointerException
.
您声明并从未初始化。只需在使用前初始化它。否则NullPointerException
。
Try to initialize it
尝试初始化它
ArrayList<PeopleDetails> people_selected= new ArrayList<PeopleDetails>();
回答by Blackbelt
you missed
你错过了
people_selected = new ArrayList<PeopleDetails>();
you have declared it but not intialized.
您已声明但未初始化。
回答by laalto
The code shows that you declare the variable but doesn't show that you initialize it, like this:
代码显示您声明了变量,但没有显示您初始化它,如下所示:
people_selected = new ArrayList<PeopleDetails>();
回答by Phant?maxx
You declared people_selectedbut you're using selected_people?!
Which is never filled...
您声明了people_selected但您使用的是selected_people?!
永远不会被填满...
回答by Abdullah Khan
The enhanced for loop is roughly of this structure
增强的for循环大致就是这个结构
for (Iterator<T> i = someList.iterator(); i.hasNext();) {
}
An un-initialized Collection ArrayList<PeopleDetails> selected_people;
refers to null
.
未初始化的 CollectionArrayList<PeopleDetails> selected_people;
指的是null
。
If you start an enhanced for loop on an un-initialized Collection it will throw NullPointerException
because of the fact that it calls the iterator someList.iterator()
on the null reference.
如果在未初始化的 Collection 上启动增强的 for 循环,它会抛出异常,NullPointerException
因为它someList.iterator()
在空引用上调用迭代器。
On the other hand if you have an initialized Collection like so
另一方面,如果你有一个像这样初始化的集合
ArrayList<PeopleDetails> selected_people = new ArrayList<>();
you will notice that the enhanced for loop doesn't throw any NullPointerException
because of the fact that someList.iterator()
now returns an iterator and i.hasNext()
returns false
just so that the loop doesn't proceed.
您会注意到增强的 for 循环不会抛出任何内容,NullPointerException
因为someList.iterator()
现在返回一个迭代器并i.hasNext()
返回false
只是为了使循环不继续。
PS:The enhanced for loop skeleton is taken from here.
PS:增强的for循环骨架取自here。