这是我的代码。我刚读的文件首先是数字,然后是字符串,它们的数字等于第一行的数字。
import java.io.*;
import java.util.*;
class groupmember
{
int recieving;
int giving;
String name;
groupmember()
{
recieving=0;
giving=0;
//name=null;
}
public void setname (String Title)
{
this.name = new String(Title);
}
public void setrecieving(int val)
{
recieving=val;
}
public void setgiving(int val)
{
giving=val;
}
public String getname()
{
return name;
}
public int getrecieving()
{
return recieving;
}
public int getgiving()
{
return giving;
}
}
class gift1 {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
BufferedReader f=new BufferedReader(new FileReader("gift1.in"));
PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter("gift1.out")));
StringTokenizer st=new StringTokenizer(f.readLine());
int NP=Integer.parseInt(st.nextToken());
int excpectedgived=0,div=0;
groupmember []groupmember=new groupmember[NP];
for(int i=0;i<NP;i++)
{
st=new StringTokenizer(f.readLine());
String name=st.nextToken();
groupmember[i].setname(name);
System.out.println(name);
}
out.close();
}
}
问题出现在这一行:
groupmember[i].setname(name);
它导致NullPointerException
. 我想知道为什么会这样。