Reputation:
The code compiles fine so I thought i was doing this right, but when I called toString, I got a bunch of null and zero values from the no-argume constructor, and when I called the method totalScoresOfHomeworks to see what value it would return, i get the error
"FException in thread "main" java.lang.NullPointerException
at GradeApplication.getTotalScoresOfHomeworks(Student.java:119)
at Student.main(Student.java:55)"
But it is not a compiling error There may or may not be something wrong with how I used "split"
Here's my code:
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;
}
}
Upvotes: 0
Views: 533
Reputation: 3067
Problem is with this code:
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;
}
homework variable is null and you are trying to split it. So it is generating error.
Second problem is with
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;
}
your are declaring these variable again but in code you are using global variable so all are null. Remove declaration part from this.
Once you fix this you will get problem here again,
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;
}
token1 is string array and you are trying to parse it in double. So it will cause run time exception.
Exception in thread "main" java.lang.NumberFormatException: For input string: "eng" at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at GradeApplication.getTotalScoresOfHomeworks(Student.java:107) at Student.main(Student.java:52)
Check your code thoroughly.
Upvotes: 0
Reputation: 1193
Here in homework variable it is not getting any value , so it can not split any value.And it it is null. So you are getting NullPointerException at tokens1 = homework.split(" ");
in this method public double getTotalScoresOfHomeworks()
.
Upvotes: 0
Reputation: 155
In your parameterised constructor you are assigning parameters to local variables. All the your class variables are not set by this. Hence throwing null pointers exception. Try assigning parameters to actual class variables. Like
public GradeApplication(String nme, int ID, String hw, String lbs, String tsts, double proj, double disc) {
name = nme;
id = ID;
homework = hw;
labs = lbs;
tests = tsts;
project = proj;
discussion = disc;
}
Upvotes: 1
Reputation: 15122
In your constructor, you don't need to declare variables again
public GradeApplication(String nme, int ID, String hw, String lbs, String tsts, double proj, double disc)
{
name=nme;
id=ID;
homework=hw;
labs=lbs;
tests=tsts;
project=proj;
discussion=disc;
}
Upvotes: 2