Hello,
I´m trying to do the exercise of Lab 1014 and I´ve got a problem.
Here is my code.
////////STUDENT CLASS///////
public class Student {
private StudentRecord studentRecord;
private int studentID;
private static int studentCount = 0;
public Student(String name, double mathG, double englisG, double
scienceG, int id){
StudentRecord st=new StudentRecord();
st.setName(name);
st.setMathGrade(mathG);
st.setEnglishGrade(englisG);
st.setScienceGrade(scienceG);
this.studentID=id;
increaseStudentCount();
}
public StudentRecord getStudentRecord(){
return this.studentRecord;
}
public int getstudentID(){
return this.studentID;
}
public static int getStudentCount(){
return studentCount;
}
public static void increaseStudentCount(){
studentCount++;
}
/////MAIN CLASS//////
public class Main {
public static void main(String[] args){
Student student1 = new Student("Juan",9,7,5,1);
Student student2 = new Student("Maria",6,4,8,2);
Student student3 = new Student("Luis",8,4,3,3);
/**
* Printing student details
*/
System.out.println("Student ID: "+student1.getstudentID());
System.out.println("Name: "+student1.getStudentRecord().getName());
<------------ HERE IS THE ERROR
System.out.println("Total number of students:
"+Student.getStudentCount());
}
}
When I compile the main class I receive the following error on the
highlighted line.
Student ID: 1
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I guess that the problem is that since I create a new studentRecord object
inside the Student constructor when I try to access to that object it
doesn´t exists anymore or something like that. As far as I understand the
exercise I must create a Student object which creates a StudentRecord object
and then print information about both objects. If I want to use proper
encapsulation but I do not know how to do it.
Could you help me?
Regards
JA
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
To unsubscribe, reply using "remove me" as the subject.