import java.util.Scanner;
public class Student {
String studentNumber;
String studentName;
float  mathScore;
float  englishScore;

//input()方法
public void input(){
System.out.println("please enter the studentNumber :");
Scanner a=new Scanner(System.in);
String  studentNumber=a.next();

System.out.println("please enter the studentName:");
Scanner b = new Scanner(System.in);
    String studentName = b.next();
    
    System.out.println("please enter the mathScore:");
    Scanner c=new Scanner(System.in);
    int mathScore=c.nextInt();
    
    System.out.println("please enter the englishScore:");
    Scanner d=new Scanner(System.in);
    int englishScore=d.nextInt();
    
    System.out.println("the information of the students are:" );
    System.out.println("studentNumber "+"studentName "
     +"mathScore "+"englishScore ");
    System.out.println(studentNumber+" "+studentName+" "
     +mathScore+" "+englishScore);
    }

// 构造方法
public Student(){
this("", "", 0.0f, 0.0f);
}
public Student(String newStudentNumber,
String newStudentName, float newMathScore,float newEnglishScore){
studentNumber=newStudentNumber;
studentName=newStudentName;
mathScore=newMathScore;
englishScore=newEnglishScore;
}

//total()方法
public float total(){
float totalScore;
totalScore=mathScore+englishScore;
System.out.println("the totalScore of the student is:"+totalScore);
return 0;
}
      
      public static void main(String args[]){
Student studentArry[];
studentArry=new Student[3];

studentArry[0].input();
studentArry[0].total();

studentArry[1].input();
studentArry[1].total();

studentArry[2].input();
studentArry[2].total();

}
}为什么编译的时候出现Exception in thread "main" java.lang.NullPointerException
at student.Student.main(Student.java:58)
的错误呢?
是哪个出现了空指针?

解决方案 »

  1.   

    studentArry=new Student[3]; 只是创建了数组,并没有创建数组里需要保存的对象
    加上
     for(int i=0;i<studentArry.length;i++){
                studentArry[i]=new Student();
            }
      

  2.   

    public static void main(String args[]){
    Student studentArry[];
    studentArry=new Student[3];studentArry[0].input();
    studentArry[0].total();studentArry[1].input();
    studentArry[1].total();studentArry[2].input();
    studentArry[2].total();}

    studentArry[0]你没有实例化 ,结果当然是null了
    你应该 studentArry[0] = new Student();
    然后 
    studentArry[0].input();
    studentArry[0].total();
      

  3.   

    恩 明白意思了
    还出现了一个问题为什么每个 学生输出的成绩都是0呢?
    totalScore=mathScore+englishScore; 没有起作用吗?
      

  4.   

    Student数组里的对象在构造时已经将其成员mathScore和englishScore的值设为了0.0
    而从输入中获得的值则赋给了input()方法里的局部变量,Student对象里的成员的值没有改变。