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
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.
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
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(); } }
mohamed_mashood (June 27th, 2013)
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
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.
mohamed_mashood (June 27th, 2013)
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.
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
Please reply me
thank you
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.
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.