public static void main(String[] args)
{
student1[] std=new student1[3];
int i=0;
for( i=0;i<3;i++)
std[i].InputData();
for( i=0;i<3;i++)
std[i].showData();
}student1是自定的一个类,使用的是默认的构造函数,
自己明明用new开辟了空间:student1[] std=new student1[3];
怎么在输入输入的时候:for( i=0;i<3;i++)std[i].InputData();还会提示java.lang.NullPointerException???
{
student1[] std=new student1[3];
int i=0;
for( i=0;i<3;i++)
std[i].InputData();
for( i=0;i<3;i++)
std[i].showData();
}student1是自定的一个类,使用的是默认的构造函数,
自己明明用new开辟了空间:student1[] std=new student1[3];
怎么在输入输入的时候:for( i=0;i<3;i++)std[i].InputData();还会提示java.lang.NullPointerException???
想要使用的时候,里面仍然是空的,所以需要把每个实例都创建出来才可以使用。
{ student1[] std=new student1[3];
int i=0;
for( i=0;i<3;i++) {
std[i] = new student1();//加上这句,给每一个元素赋值,否则你没有初始化,默认的为null
std[i].InputData();
}
for( i=0;i<3;i++)
std[i].showData();}
std[i] = new student1();