當我嘗試在學生類中調用AddGrade
方法時,該方法使用的是年級中的AddGrade
,我得到一個NullPointerException
。 它說我必須添加更多的細節,我不知道爲什麼。在eclipse中得到一個空指向異常java
一級是
public class Grades {
private double qualPts;
private int numCreds;
public Grades() {
}
/**
* Returns the GPA
*/
public double getGPA()
{
if (numCreds>0)
return qualPts/numCreds;
return 0;
}
/**
* Adds the new grade's credits
* Weights the newly added grade
*/
public void addGrade(int creds, double grade)
{
numCreds+=creds;
qualPts+=creds*grade;
}
/**
* Returns the number of credits
*/
public int getNumCred()
{
return numCreds;
}
}
類2是
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 the new grade's credits
* weights the new grade
*/
public void addGrade(int creds, double grade)
{
grades.addGrade(creds,grade);
}
}
看看'Student.grades'。仔細看看它,很難。你昨天也問過這個。 –
另外,請查看使用調試器並遍歷代碼以瞭解其工作原理。 –
除此之外,在詢問關於你得到的異常時,你應該總是追加完整的堆棧跟蹤。 –