getting a null pointing exception in eclipse java -
when try call addgrade
method in student class, uses addgrade
in grades class nullpointerexception
. says have add more details i'm not sure why.
class 1 is
public class grades { private double qualpts; private int numcreds; public grades() { } /** * returns gpa */ public double getgpa() { if (numcreds>0) return qualpts/numcreds; return 0; } /** * adds new grade's credits * weights newly added grade */ public void addgrade(int creds, double grade) { numcreds+=creds; qualpts+=creds*grade; } /** * returns number of credits */ public int getnumcred() { return numcreds; } }
class 2 is
public class student { private string name; private string bnumber; private grades grades; public student(string name, string bnumber) { this.name=name; this.bnumber=bnumber; } /** * adds new grade's credits * weights new grade */ public void addgrade(int creds, double grade) { grades.addgrade(creds,grade); }
}
in constructor in student class, need create instance of grades.
public student(string name, string bnumber) { this.name=name; this.bnumber=bnumber; this.grades = new grades(); }
otherwise this.grades
null, because never set value.
Comments
Post a Comment