新手的提问。。。

qq8574157 2008-04-16 01:00:22
import java.io.*;
class Student
{
private int num;
private String name;
private double score;
public void setImfo() throws Exception
{
InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(isr);
System.out.print("num:");
num=Integer.parseInt(br.readLine());
System.out.print("name:");
name=br.readLine();
System.out.print("score:");
score=Double.parseDouble(br.readLine());
}
public double reScore()
{
return score;
}
}
public class Test
{
public static void main(String [] args) throws Exception
{
Student [] stu= new Student [5];
double aver=0;
for(int i=0;i<stu.length;++i)
{
stu[i].setImfo(); //Exception in thread "main" java.lang.NullPointerException------此处为什么会出现这个错误?原因以及解决的方法??
aver+=stu[i].reScore();
}
System.out.println("aver= "+aver/stu.length);
}
}
...全文
51 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq8574157 2008-04-16
  • 打赏
  • 举报
回复
嗯 明白了 谢谢了哦。。。
chen09 2008-04-16
  • 打赏
  • 举报
回复
Student [] stu= new Student [5];
只是stu初始化了,stu[0-4]还都是null,你没有初始化数组的元素呀。
加一句 stu[i]= new Student();

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧