代码编译得很好,所以我认为我这样做是正确的,但是当我调用 toString 时,我从无参数构造函数中得到了一堆空值和零值,当我调用方法 totalScoresOfHomeworks 来查看它会返回什么值时,我得到了错误
"FException in thread "main" java.lang.NullPointerException
at GradeApplication.getTotalScoresOfHomeworks(Student.java:119)
at Student.main(Student.java:55)"
但这不是编译错误我使用“拆分”的方式可能有问题,也可能没有问题
这是我的代码:
import java.util.Scanner;
import java.io.*;
public class Student
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
//Define variables
String name;
int id;
String homework;
String labs;
String tests;
//int[] quizzesArray= new int[3];//**NO QUIZ GRADES
double[] homeworkArray=new double[10];
double[] labsArray= new double[6];
double[] testsArray=new double[3];
double project;
double discussion;
System.out.print("What is your name? ");
name=keyboard.nextLine();
System.out.print("\nWhat is your student ID? ");
id=keyboard.nextInt();
//Create strings to tokenize homework grades
homework = keyboard.nextLine();
System.out.println("\nPlease enter homework grades separated by spaces:");
homework = keyboard.nextLine();
//creat object to pass all info to class
System.out.println("Please enter lab grades separated by spaces:");
labs = keyboard.nextLine();
System.out.println("Please enter test grades separated by spaces:");
tests = keyboard.nextLine();
System.out.println("Please enter project grade:");
project = keyboard.nextDouble();
System.out.println("Please enter discussion grade:");
discussion = keyboard.nextDouble();
System.out.println("\nResults: ");
//Call toString method
GradeApplication ga = new GradeApplication(name,id,homework,labs,tests,project,discussion);
System.out.print(ga.toString());
System.out.print(ga.getTotalScoresOfHomeworks());
}
}
class GradeApplication
{
//Define variables
String name;
int id;
String homework;
String labs;
String tests;
double project;
double discussion;
//No argument constructor
public GradeApplication()
{
name="";
id=0;
homework="";
labs="";
tests="";
project=0.0;
discussion=0.0;
}
//Parameter constructor
public GradeApplication(String nme, int ID, String hw, String lbs, String tsts, double proj, double disc)
{
String name=nme;
int id=ID;
String homework=hw;
String labs=lbs;
String tests=tsts;
double project=proj;
double discussion=disc;
}
//Attributes
private double totalScoresOfHomeworks;
private double totalScoresOfLabs;
private double totalScoresOfTests;
private double scoreOfProject;
private double scoreOfDiscussion;
//Methods
public double getTotalScoresOfHomeworks()
{
double[] homeworkArray=new double[10];
double sum1=0;
String[] tokens1;
tokens1 = homework.split(" ");
for (int i=0;i<9; i++)
{
homeworkArray[i]=Double.parseDouble(tokens1[i]);
sum1+=homeworkArray[i];
}
return sum1;
}
public void setTotalScoresOfHomeworks(double hwk)
{
totalScoresOfHomeworks=hwk;
}
public double getTotalScoresOfLabs()
{
double[] labsArray= new double[6];
double sum2=0;
String[] tokens2;
tokens2 = labs.split(" ");
for (int j=0;j<9; j++)
{
labsArray[j]=Double.parseDouble(tokens2[j]);
}
return sum2;
}
public void setTotalScoresOfLabs(double labs)
{
totalScoresOfLabs=labs;
}
public double getTotalScoresOfTests()
{
double[] testsArray=new double[3];
double sum3=0;
String[] tokens3;
{
tokens3 =tests.split(" ");
for (int k=0;k<9; k++)
{
testsArray[k]=Double.parseDouble(tokens3[k]);
}
return sum3;
}
public void setTotalScoresOfTests(double tests)
{
totalScoresOfTests=tests;
}
public double getScoreOfProject()
{
return project;
}
public void setScoreOfProject(double project)
{
scoreOfProject=project;
}
public double getscoresOfDiscussion()
{
return discussion;
}
public void setScoresOfDiscussion(double disc)
{
scoreOfDiscussion=disc;
}
public double getTotalScores()
{
return (totalScoresOfHomeworks+totalScoresOfLabs+totalScoresOfTests+scoreOfProject+scoreOfDiscussion);
}
public double getPercentage()
{
return 100*getTotalScores()/680;
}
public char getGrade()
{
char grade=0;
if (getPercentage() >90 )
grade='A';
else if (getPercentage()>80)
grade='B';
else if (getPercentage()>70)
grade='C';
else if (getPercentage()>60)
grade='D';
else if (getPercentage() <= 60)
grade='F';
return grade;
}
public String toString()
{
String str="Student name:\t"+ name +
"\nStudent ID:\t" + id +
"\nTotal scores:\t" +getTotalScores()+
"\nMax scores:\t" + 680 +
"\nPercentage:\t"+ getPercentage() +
"\nGrade:\t" + getGrade();
return str;
}
}