0

In the below code , I'm trying to calculate the GPA of the undergraduate given the grade point and credit values. But how Can I invoke the calGPA method in the main method ? CalGPA should be an abstract method so i cannot make it static because I use JDK 7 in my exams. Is there a possible solution. I think my CalGPA logic is wrong too... I'm getting this Exception when I try to invoke getGradePoints method. why is that? Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because the return value of "Undergraduate.getResult()" is null

public abstract class Student{

    private String name;
    private String courseName;
    private String result;
    private int creditValue;
    private String regNo;
    private double gradePoint;
    private String degree;
    String grade;
    
    Student(String name, String regNo,String degree){
        this.name = name;
        this.regNo = regNo;
        this.degree = degree;
    }
    
    Student( String courseName,int creditValue, String result){
        this.courseName = courseName;
        this.creditValue = creditValue;
        this.result = result;
    }
    
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setCourseName(String courseName){
        this.courseName = courseName;
    }
    public String getCourseName(){
        return courseName;
    }
    public void setResult(String result){
        this.result = result;
    }
    public String getResult(){
        return this.result;
    }
    public void setCreditValue(int creditValue){
        this.creditValue = creditValue;
    }
    public int getCreditValue(){
        return this.creditValue;
    }
    public void setRegNo(String regNo){
        this.regNo = regNo;
    }
    public String getRegNo(){
        return this.regNo;
    }
    public void setGradePoint(double gradePoint){
        this.gradePoint = gradePoint;
    }
    public double getGradePoint(){
        return this.gradePoint;
    }
    public abstract void calGPA(Student[] st);

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }


}

public class Undergraduate extends Student{

    Undergraduate(String name,String regNo,String degree){
        super(name,regNo,degree);
    }
    Undergraduate(String courseName,int creditValue, String result){
        super(courseName,creditValue,result);
    }
    
    public void calGPA(Student[] under){
        int totalCredits = 0;
        double sumOf = 0;
        for(int i =0;i<under.length;i++) {
            sumOf += under[i].getGradePoint()*under[i].getCreditValue();
            totalCredits = totalCredits + under[i].getCreditValue();
        }
        double GPA = sumOf/totalCredits;
        System.out.println("GPA : "+GPA);
    }
    
    public static void undergradDetails(Student[] under, Undergraduate u1){
    System.out.println("********Student Information*************");
    System.out.println("Name : "+u1.getName());
        System.out.println("Registration Number : "+u1.getRegNo());
        System.out.println("Degree: "+u1.getDegree());
        System.out.println("Subject\tGrade");
        for(int i =0;i<under.length;i++){
            System.out.println(" "+under[i].getCourseName() + " \t"+under[i].getResult());
        }
    }
    public double getGradePoints(Student[] under){
        if(getResult().equals("A+")){
            setGradePoint(4.25);
        }if(getResult().equals("A")){
            setGradePoint(4.00);
        }if(getResult().equals("A-")){
            setGradePoint(3.75);
        }if(getResult().equals("B+")){
                setGradePoint(3.50);
            }if(getResult().equals("B")){
                setGradePoint(3.25);
            }if(getResult().equals("B-")){
                    setGradePoint(3.00);
                }if(getResult().equals("C+")){
                        setGradePoint(2.75);
                }if(getResult().equals("C")){
                    setGradePoint(2.50);
                }if(getResult().equals("C-")){
                    setGradePoint(2.25);
                }if(getResult().equals("D")){
                    setGradePoint(1.25);
                }if(getResult().equals("F")){
                    setGradePoint(0.00);
                }
        return getGradePoint();
             }
    
    
        }

public class Driver{

    public static void main(String[] args){
        
        Undergraduate u1 = new Undergraduate("Amila Gunathilake","UG/2011/10045","BSc");
        Student[] under = new Undergraduate[6];
        under[0] = new Undergraduate("CS105",1,"A");
        under[1] = new Undergraduate("CS102",3,"B-");
        under[2] = new Undergraduate("ST104",1,"C");
        under[3] = new Undergraduate("CH109",3,"A-");
        under[4] = new Undergraduate("CH106",2,"A+");
        under[5] = new Undergraduate("CS103",2,"A");

        under.calGPA(under);
         System.out.println(u1.getGradePoints(under));
Allen Hay
  • 81
  • 1
  • 7
  • 1
    You are calling `getGradePoints` on `u1` which is entirely unrelated to the `under` array passed to `calGPA`. Since no result is ever set on `u1`, it is `null`, causing a `NullPointerException`. – Holger Jan 06 '22 at 17:42

1 Answers1

0

You should call u1.calGPA(under) rather than calGPA(under) to make your program run basically not having compilation error.

Added: There are a lot of problems with your code.

  1. Your constructor signature is something

    Undergraduate(String courseName, int creditValue, String result)

which is fine later you are initializing your constructor

new Undergraduate("CS105", 1, "A")

Then in calGPA function you are multiplying gradePoint to creditvalue and save that.

under[i].getGradePoint() * under[i].getCreditValue();

that is always summing up as 0.

Zahid Khan
  • 2,130
  • 2
  • 18
  • 31