【问题标题】:ArrayList null pointer exceptionArrayList 空指针异常
【发布时间】:2014-02-16 10:54:06
【问题描述】:

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

ArrayList<PeopleDetails> 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 people_selected = new ArrayList();
  • 您在 declation 和添加到项目部分时为 arraylist 使用不同的变量名?
  • 向一个对象声明一个引用变量只不过是为您保留空间以将其设置为一个真实对象的引用在将来的某个时间。在您将其设置为引用之前,它不会指向任何内容或 null,这会在您尝试取消引用它时导致空指针异常(纯粹主义者,我知道统一化的空引用指向空对象,但为了简单的注释....)

标签: java android listview arraylist nullpointerexception


【解决方案1】:

你错过了

people_selected = new ArrayList<PeopleDetails>();

你已经声明了它但没有初始化。

【讨论】:

    【解决方案2】:

    代码显示你声明了变量,但没有显示你初始化它,像这样:

    people_selected = new ArrayList<PeopleDetails>();
    

    【讨论】:

      【解决方案3】:
      ArrayList<PeopleDetails> people_selected;
      

      您已声明但从未初始化。只需在使用前对其进行初始化。否则NullPointerException.

      尝试初始化它

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

      【讨论】:

        【解决方案4】:

        您声明了 people_selected,但您使用的是 selected_people?!
        哪个永远不会被填满……

        【讨论】:

        • 这只是问题中的一个错字......不过谢谢!我已经纠正了。 :)
        【解决方案5】:

        增强的for循环大致就是这样的结构

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

        未初始化的集合ArrayList&lt;PeopleDetails&gt; selected_people; 引用null

        如果您在未初始化的 Collection 上启动增强的 for 循环,它将抛出 NullPointerException,因为它在 null 引用上调用迭代器 someList.iterator()

        另一方面,如果你有一个这样的初始化集合

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

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

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

        【讨论】:

          【解决方案6】:

          这个错误是因为你没有初始化数组

          添加这个解决了我的问题

          selected_people = new ArrayList<PeopleDetails>();
          

          【讨论】:

            猜你喜欢
            • 2023-03-05
            • 1970-01-01
            • 2014-07-19
            • 1970-01-01
            • 2016-06-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多