尝试将新类实例添加到 ArrayList 时,while 循环中出现 NullPointerException [英] NullPointerException in while loop when trying to add new Class instances to ArrayList

查看:22
本文介绍了尝试将新类实例添加到 ArrayList 时,while 循环中出现 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌上搜索得越多,我就越困惑.

The more I google this the more confused I'm getting.

我从 CSV 中引入了一个未知长度的名称列表和一些其他详细信息,然后我需要将其转换为 Person 对象并存储在名为 people 的列表中,该列表是类 Club 的实例变量,一个其成员名单基本上.

I'm bringing in a list of names of unknown length with some other details in from a CSV which I then need to turn into Person objects and store in a list called people which is the instance variable for the class Club, a list of its members basically.

这是我需要做的更复杂的事情的一个非常简化的版本,我需要在其中循环遍历一个文件,为每一行创建对象,然后我需要将其添加到列表集合中.

This is a very simplified version of something more complex I need to do in which I need to while loop through a file creating objects for each line which I then need to add to a list collection.

当我运行我的代码时,我不断收到 nullPointerException 错误,我很难过如何避免它.我猜我创建新对象时的变量 p 需要在每个循环中更改,但我认为不可能动态更改变量,是吗?

I keep getting a nullPointerException error when I run my code though and I'm stumped how to avoid it. I'm guessing that my variable p when I create the new object would need to change on each loop but I don't think it's possible to change variables dynamically is it?

想不出如何每次都使用有效的非空引用将对象提交到集合中.将不胜感激任何帮助.我已尝试删除下面代码中所有不必要的内容.

Can't think how I can commit the object to the collection with a valid non null reference each time. Would be very grateful for any help. I've tried to cut out all the unnecessary stuff in the code below.

谢谢

   //class arraylist instance variable of class "Club"
   private ArrayList<Person> people;

   //class constructor for Club
   public Club()
   {List<Person> people = new ArrayList<>();}

   public void readInClubMembers()
   {
      //some variables concerning the file input
      String currentLine;
      String name;
      String ageGroup;
      while (bufferedScanner.hasNextLine())
      {
         //some lines bringing in the scanner input from the file
         name = lineScanner.next();
         ageGroup = "young";
         Person p = new Person(); // i guess things are going wrong around here
         people.add(p);
         p.setName(name);
         p.setAgeGroup(ageGroup);
      }
   }

推荐答案

去掉构造函数内 people = ... 之前的 List ,否则你是在声明一个构造函数内的新局部变量 people 遮蔽了 field people(然后从未使用过).这会使类字段未初始化 (null),然后导致 NPE.

Remove the List<Person> before people = … inside the constructor, otherwise you are declaring a new local variable people inside the constructor shadowing the field people (which is then never used). This leaves the class field uninitialized (null) and then causes the NPE.

你想要的是初始化字段people:

What you want instead is initializing the field people:

public Club() {
    // you can also use "this.people = …" to be explicit
    people = new ArrayList<>();
}

显示差异:

class Example {
    private int myNumber;

    public Example() {
        myNumber = 42; // sets the field of the class
        int myNumber = 1337; // declares a new local variable shadowing the class field
        myNumber = -13; // accesses the object in the closest scope which is the local variable
        this.myNumber = 0; // explicitly accesses the class field
    }
}

这篇关于尝试将新类实例添加到 ArrayList 时,while 循环中出现 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆