Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Access insatance variables

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Access insatance variables

    Hi all,

    Sorry for very basic question,my question is "ïf i have over ride one method of an interface,can i access the instance variable of the implemented class from the over rided method?

    Thank you


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Access insatance variables

    You access it as you would any variable: just by using it.

    interface Greeter {
        public abstract void greet();
    }
     
        /**
         * A Greeter implements which greets a specific target in 
         * English on System.out.
         */
    public class EnglishGreeter implements Greeter{
     
        private String target;
     
        public EnglishGreeter(String target) {
            this.target = target;
        }
     
        @Override 
        public void greet() {
                // we can access the target instance variable of this
                // Greeter implementation in the overridden method
                // just by using it
            System.out.printf("Hello, %s!%n", target);
        }
     
        public static void main(String[] args) {
            Greeter test = new EnglishGreeter("world");
            test.greet();
        }
    }

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    mohamed_mashood (June 27th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Access insatance variables

    Thank you,but in my project i have two override method called retrieveData() and displayData() implemented by DataManipulatorView.

    DataManipulatorView is a class and it has many text fields,i am calling retrieveData() and displayData() from another class called UpdaterView.

    After completion of retrieveData() it will call displayData() and it should set all the values to textfields.so the i use setText() on the textfield object.
    example, date_field.setText(date);

    program compiled successfully,but at runtime it throws null pointer exception on line date_field.setText(date);

    date_field and date is instance variable and normal string variable correspondingly.

    Thank you

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Access insatance variables

    What you are describing now is the same as in your other thread: http://www.javaprogrammingforums.com...interface.html

    Follow copeg's advice and find which variable (or expression) it is that is null. You will get the NullPointerException when you use the "dot" dereference operator on something that is null and you can use System.out.println() to check for that:

    System.out.println("About to call foo.bar()");
    foo.bar(); // foo might be null

    The System.out.println() is there so you can find the variable that is causing the problem. In your case you might suspect dm or one of the variables you are using for the text fields.

    ---

    Bear in mind that it is very hard for people to comment specifically on code they can't see! (In your other thread the NullPointerException is shown as happening on a line of code that is a comment.) If your code is very long, you may have to construct a small example that shows the problem without all the rest of the problem logic.

  6. The Following User Says Thank You to pbrockway2 For This Useful Post:

    mohamed_mashood (June 27th, 2013)

  7. #5
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Access insatance variables

    you are saying that i might suspect dm or one of the textfield's.

    but i have created object for all textfield's then i dont know why null pointer exception is throwing.

    I would be thankfull to you,please help me on this problem.If i want more details,then i will post full code tommorow.

  8. #6
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Access insatance variables

    I have attached two files namely DATAMANIPULATORVIEW and UPDATERVIEW, that am facing the problem

    I could not post here as the file is too long.In the line of 272 in DATAMANIPULATORVIEW class and in the line of 87 in UPDATERVIEW class i go t the null pointer exception.please refer to two files and help in which place am making a mistake.

    Please i need help,thak you

  9. #7
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Access insatance variables

    Please reply me

    thank you

  10. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Access insatance variables

    I understand how frustrating it can be not to see the way through a problem.

    If the runtime complains that a variable is null, then it really is null. You might think it was given a nonnull value, but it wasn't.

    Once you have determined which variable is null you have to go back through the code - specifically to the place you thought you had assigned a nonnull value to the textfields or whatever - and figure out why that didn't happen.

    Although I understand how frustrating it is, the problem of the null pointer exception was raised in the other thread and should be followed there and not here. To get anywhere in that thread you will have to post some actual code. And, as I commented before, it might be a good idea to step back from the actual code you are working with and construct a small example which shows the problem.

  11. #9
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Access insatance variables

    I have moved the final post by the OP to the other thread and will close this one.

    If anyone has objections to this, post in the Cafe forum. Or PM me and take your chances that I read the PMs...

    ---

    The OP has posted code in that thread. So please go there and help him! I'm on my phone at the moment and can't offer any real help until I can get to a real computer.

Similar Threads

  1. Replies: 21
    Last Post: November 27th, 2012, 10:58 PM
  2. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 08:34 PM
  3. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  4. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM