Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java we were supsoed to create a program where it read a list of grades from

ID: 3874876 • Letter: I

Question

In Java we were supsoed to create a program where it read a list of grades from a text file and then calculate a GPA but im running into a error in the code below where im getting a null pointer exeption. im not sure here im going wrong and any help would be great.

package program1;
import algs31.BinarySearchST;
import stdlib.*;

public class ComputeGPA {
public static void main(String[] args) {
BinarySearchST<String, Double> gradePointsST = new BinarySearchST<>();
       gradePointsST.put("A+", 4.33);
       gradePointsST.put("A", 4.00);
       gradePointsST.put("A-", 3.67);
       gradePointsST.put("B+", 3.33);
       gradePointsST.put("B", 3.00);
       gradePointsST.put("B-", 2.67);
       gradePointsST.put("C+", 2.33);
       gradePointsST.put("C", 2.00);
       gradePointsST.put("C-", 1.67);
       gradePointsST.put("D", 1.00);
       gradePointsST.put("F", 0.00);

       double totalGradePoints = 0.0;
       int numGrades = 0;

StdIn.fromFile("data/a1grades.rtf");

while (!StdIn.isEmpty()) {
String grade = StdIn.readString();
totalGradePoints += gradePointsST.get(grade); // running into errors here on this line.
numGrades++;
}

System.out.println("GPA = " + totalGradePoints / numGrades);
}
}

Explanation / Answer

this code is giving nullPointer exception because when your code will not satisfy while condition then print GPA but in Print statement totalGradePoint is divided by numGrades which is initially assign by 0. And this is undefined value. .

I know numGrades will increase and become positive value only when code satisfy while condition and execute while statement.

Initilally numGrades value must be assign by 1

package program1;
import algs31.BinarySearchST;
import stdlib.*;

public class ComputeGPA {
public static void main(String[] args) {
BinarySearchST<String, Double> gradePointsST = new BinarySearchST<>();
       gradePointsST.put("A+", 4.33);
       gradePointsST.put("A", 4.00);
       gradePointsST.put("A-", 3.67);
       gradePointsST.put("B+", 3.33);
       gradePointsST.put("B", 3.00);
       gradePointsST.put("B-", 2.67);
       gradePointsST.put("C+", 2.33);
       gradePointsST.put("C", 2.00);
       gradePointsST.put("C-", 1.67);
       gradePointsST.put("D", 1.00);
       gradePointsST.put("F", 0.00);

       double totalGradePoints = 0.0;
       int numGrades = 1; //changed in code here, before it is initialized by 0

StdIn.fromFile("data/a1grades.rtf");

while (!StdIn.isEmpty()) {
String grade = StdIn.readString();
totalGradePoints += gradePointsST.get(grade); // now this line will execute successfully
numGrades++;
}

System.out.println("GPA = " + totalGradePoints / numGrades);
}
}

BinarySearchST<String, Double> gradePointsST = new BinarySearchST<>();

gradePointsST is not initilize correctly that's why code calls default constructor which is NULL. So need some changes in initilization of gradePointsST

BinarySearchST<String, Double> gradePointsST = new BinarySearchST<String,Double>();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote