On Mar 26, 6:33 am, Ryu <[email protected]> wrote:
> Hi to all,
> Hope everyone is fine.Sorry i have a problem with this
> lab's homework as other people was , I also read the emails that
> previously posted regarding this lab's homework but i still unable to
> do it .I stuck at a point that how we access the StudentRecord class
> setName() method using Student class object.First i try this. I have
> only set and getStudentId methods in my Student class and also
> increasecount method
>
> // Create an object instance of Student
> class.
> Student annaStudent =new Student();
>
> // Increament the studentCount by
> invoking a static method.
> Student.increaseStudentCount();
>
> annaStudent.studentRecord.setName("Anna");
>
> but it shows error that studentRecord has private access in Student
> class. The same thing was follow by another group member before and in
> reply of this the answer is you initialize student record like this
>
> annaStudent.studentRecord = new
> StudentRecord();
>
> but when i try this it again says that studentRecord has private
> access in Student class.
>
> Secondly i follow another group member technique and
> that is make setter and getter method in Student class like this
>
> public void setStudentName
> (String name){
>
> studentRecord.setName(name);
> }
>
> public String
> getStudentName(){
> return
> studentRecord.getName();
> }
>
> but when i call it in main like
>
> annaStudent.setStudentName("Anna");
>
> annaStudent.setStudentId(1);
>
> and run the program i got an error message that
>
> Exception in thread "main" java.lang.NullPointerException
> at Student.setStudentName(Student.java:46)
> at MyOwnProjectMain.main(MyOwnProjectMain.java:27)
>
> At (Student.java:46) i have this line
>
> studentRecord.setName(name);
>
> and (MyOwnProjectMain.java:27) i have this one
>
> annaStudent.setStudentName("Anna");
>
You had tried two ways. In the first: You had tried to access a non
visible member. Whereby, in the second: You had tried to access a
member that has not been initialised.
> So please you people tell me that how can i access StudentRecord
> setName() method using Student class object
First, initialise studentRecord. Then, you can access it in <main>
like you did before. For example:
In student:
// Initialise studentRecord.
Student(){
studentRecord=new StudentRecord();
}
>
> Second, studentRecord is private and for private we use setter and
> getter methods , i want to ask that is it necessary to made setter and
> getter for studentRecord or we can access StudentRecord methods
> without making setter and getter method in Student class
>
> Third why i got null pointer exception
You had access studentRecord when it has not been initialised.
>
> thanks in advance
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---