NullPointerException异常在while循环试图添加新的Class实例ArrayList的时候 [英] NullPointerException in while loop when trying to add new Class instances to ArrayList
问题描述
我越google一下,我发现了更多的困惑。
我把在从CSV其他一些细节,然后我需要转成Person对象并存储在一个名为名单的人是该类俱乐部实例变量,一个未知长度的名称列表其成员基本上是列表。
这是一个更复杂的东西,我需要做的,我需要通过文件创建的每一行对象,然后我需要添加到列表集合while循环非常简化的版本。
当我运行我的code,虽然我很为难如何避免它,我不断收到NullPointerException错误。我猜,我的变量p当我创建新的对象都需要更改每个循环,但我不认为这是有可能改变的变量动态是什么呢?
没想到我怎么能每次都提交对象集合了有效的非空引用。将是任何帮助非常感激。我试图削减了所有不必要的东西在下面的code。
感谢您
类俱乐部//类ArrayList实例变量
私人的ArrayList<&人GT;人; //类构造函数俱乐部
市民俱乐部()
{列表与LT;人>人=新的ArrayList<>();} 公共无效readInClubMembers()
{
//有关文件输入一些变量
串currentLine;
字符串名称;
串ageGroup;
而(bufferedScanner.hasNextLine())
{
//一些线将在扫描器输入从文件
名称= lineScanner.next();
ageGroup =年轻;
人p =新的Person(); //我想事情会错在这里
people.add(P);
p.setName(名);
p.setAgeGroup(ageGroup);
}
}
删除列表<人>
在人= ...
在构造函数中,否则,您声明一个新的局部变量人
构造阴影中的字段的里面的人
(这是后不再使用)。这使得类字段未初始化(空
),然后导致NPE。
您想取而代之的是初始化领域是什么人
:
公开俱乐部(){
//你也可以使用this.people = ...要明确
人=新的ArrayList<>();
}
以示区别:
类示例{
私人诠释mynumber的; 公共示例(){
为mynumber = 42; //设置类领域
INT mynumber的= 1337; //声明了一个新的局部变量阴影类领域
为mynumber = -13; //访问在最接近范围的对象,它是局部变量
this.myNumber = 0; //明确地访问类的字段
}
}
The more I google this the more confused I'm getting.
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.
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.
Thank you
//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);
}
}
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.
What you want instead is initializing the field people
:
public Club() {
// you can also use "this.people = …" to be explicit
people = new ArrayList<>();
}
To show the difference:
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
}
}
这篇关于NullPointerException异常在while循环试图添加新的Class实例ArrayList的时候的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!