我得到一个不寻常的,
NullPointerException: Exception in thread "main" java.lang.NullPointerException.
我自己的TeamClass
:
public DSALinkedList<String> players;
private String name, contactName, ContactAddr, contactEmail, parent;
public Team(String inName, String inConName, String inConAddr, String inConEmail, String inParent)
{
name = inName;
DSALinkedList<String> players = new DSALinkedList<String>(); //Constructs an empty players list using Strings as the value.
parent = inParent;
contactName = inConName;
ContactAddr = inConAddr;
contactEmail = inConEmail;
}
//使用方法
public void addPlayer(String inPlayer)
{
players.insertFirst(inPlayer);
}
然后在我的主课程中,我从文件构建一个团队,调用这个方法:
public static Team constructTeam(String inLine)
{
String[] lineArray = new String[16];
lineArray = inLine.split(",");
int i;
String name, contact, email, address, parent;
name = lineArray[1];
parent = lineArray[2];
contact = lineArray[3];
email = lineArray[4];
address = lineArray[5];
Team team = new Team(name, contact, address, email, parent);
System.out.println(team.getName());
//这是一个完整性检查,这会正确打印团队名称,这对我来说显示了团队&#39;不是空的。
//将玩家(字符串)添加到玩家列表中。 &#34; PLAYER:FirstName LastName&#34;
for(i=6; i<15; i++) //First player in lineArray to potentially the last.
{
if(!lineArray[i].equals(""))
{
System.out.println("Test:"+ lineArray[i]);
String player = lineArray[i];
team.addPlayer(player); //ERROR OCCURS HERE
}
}
return team;
}
答案 0 :(得分:0)
您正在构造函数DSALinkedList<String> players
中的此行上重新声明您的类变量DSALinkedList<String> players = new DSALinkedList<String>();
,
应该只有 - players = new DSALinkedList<String>();
因此,在下面的方法中,players
因其类变量而为NULL。
public void addPlayer(String inPlayer) {
players.insertFirst(inPlayer);
}
答案 1 :(得分:0)
将通过以下方式使用播放器:
public DSALinkedList<String> players;
private String name, contactName, ContactAddr, contactEmail, parent;
public Team(String inName, String inConName, String inConAddr, String inConEmail, String inParent)
{
name = inName;
players = new DSALinkedList<String>(); //Constructs an empty players list using Strings as the value.
parent = inParent;
contactName = inConName;
ContactAddr = inConAddr;
contactEmail = inConEmail;
}