On Mar 28, 4:10 am, Basit Mahmood <[email protected]> wrote: > Hi to all, > Thanks for your help .It really helps me a lot.Although you told > me that how to do this homework but after reading your replies i m also > facing problem in doing that but atlast i did it but i want to know that is > my approach right? and if there is any other alternative please tell me . > Here what i did > > As you told me to initialize StudentRecord i did it > > public class Student { > /**Create an instance of Student class */ > public Student(){ > studentRecord = new StudentRecord(); > } > > Then in the same class(Student) i wrote setter and getter method in this way > > public String getName(){ > return studentRecord.getName(); > } > > public void setName(String name){ > studentRecord.setName(name); > } > > and then in the main class(in my case MyOwnProjectMain) I set and call it in > this way > > annaStudent.setName("Anna"); > > System.out.println("Name = " + annaStudent.getName()); > > Is this approach is right? . My question is can we do it without creating > the above setName and getName method in Student class > Actually in the first place I initialize studentRecord as you told me in the > Student constructor then without writing the above setter and getter > methods(setName and getName) in Student class i try to access the name in > this way > > annaStudent.studentRecord.setName("Anna"); > > But it again says that studentRecord has private access in Student class > > Also as Vasile says that try > > StudentRecord rec = annaStudent.getStudentRecord(); > > I also did it but it syas no symbol found in Student class then i replace > the getStudentRecord() to getName() and setName() but it again says that no > setName and getName method in Student class. > > So please tell me is it necessary to create these setName and getName method > that i did, in the Student class or is there any alternative to do it > .Thanks for your help Or you can do it as Vasile suggested. For example, in student: // Expose studentRecord. Then you can access studentRecord from main as in above. public StudentRecord getStudentRecord(){ return studentRecord; } > > On Thu, Mar 26, 2009 at 10:56 AM, * ^ * <[email protected]> wrote: > > > > > 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 -~----------~----~----~----~------~----~------~--~---
