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)
的错误呢?
是哪个出现了空指针?
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)
的错误呢?
是哪个出现了空指针?
加上
for(int i=0;i<studentArry.length;i++){
studentArry[i]=new Student();
}
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();
还出现了一个问题为什么每个 学生输出的成绩都是0呢?
totalScore=mathScore+englishScore; 没有起作用吗?
而从输入中获得的值则赋给了input()方法里的局部变量,Student对象里的成员的值没有改变。