This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes making a class visible Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "making a class visible" Watch "making a class visible" New topic
Author

making a class visible

Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
I have a container class called LimitedTextContainer, I have a document class class called LimitedTextDocument, and I have a Values class that just contains the getter/setters for variables value, minimum, and maximum. I had the document class call Values and do the work it needed to with those variable. However, my instructor wants Values visible in the container class even though it is used in the document class. This is what I need help with because I can't seem to make that happen. Here is what I have now in the container class to make Values visible:
String max = "55";
String min = "35";
Values vals = new Values();
LimitedTextDocument doc = (LimitedTextDocument) myText.getDocument();
Here is what I have in my document class:
vals.setMinimum(min);
vals.setMaximum(max);
vals.setValue(str);
total = total + vals.getValue();
The compile error that I get is "vals cannot be resolved".
Howard Kushner
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 361
Candy,
Please show us the classes and methods to which these snippets of code belong. It looks like the problem has to do with the scope of variables.
Regards,


Howard Kushner<br />IBM Certified Enterprise Developer - WebSphere Studio Application Developer V5.0<br />IBM Certified Advanced System Administrator - WebSphere Application Server V5.0<br />IBM Certified Solution Developer - Web Services with WebSphere Studio V5.1<br /><a href="http://www.amazon.com/exec/obidos/tg/detail/-/1931182108/" target="_blank" rel="nofollow">Developing J2EE Applications with WebSphere Studio</a> my Certification Study Guide for IBM Test 287
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
Ok, I have gotten this to compile with removing the comment marks in front of Values vals; in the document class and commenting out vals.setMinimum(min) and vals.setMaximum(max). I now can compile and run the code but I get a NullPointerException at vals.getValue(). Any advice on how to fix this?
Howard Kushner
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 361
Candy,
I need one more bit of information. It appears as though there in a class called JLimitedText referenced in the LimitedTextContainer class, for which I do not have the source. Can you share it?
Regards,
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123

Howard Kushner
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 361
Well Candy we're making great progress, but we're not quite done. Are we having fun yet? I sure am!
Okay, back to the task at hand... looks like there's a LimitedTextEvent class and LimitedTextListener interface. If you're still game, then I am too. Just show us the source for these two critters and we'll take it from there!
I reckon we're almost done.
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
There is also LimitedTextTest that is the driver for this whole thing. Would you like to see that too?
Howard Kushner
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 361
Originally posted by Candy Bortniker:
There is also LimitedTextTest that is the driver for this whole thing. Would you like to see that too?

Yep.
Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
Howard Kushner
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 361
Originally posted by Candy Bortniker:
I have a container class called LimitedTextContainer, I have a document class class called LimitedTextDocument, and I have a Values class that just contains the getter/setters for variables value, minimum, and maximum. I had the document class call Values and do the work it needed to with those variable. However, my instructor wants Values visible in the container class even though it is used in the document class. This is what I need help with because I can't seem to make that happen. Here is what I have now in the container class to make Values visible:
String max = "55";
String min = "35";
Values vals = new Values();
LimitedTextDocument doc = (LimitedTextDocument) myText.getDocument();
Here is what I have in my document class:
vals.setMinimum(min);
vals.setMaximum(max);
vals.setValue(str);
total = total + vals.getValue();
The compile error that I get is "vals cannot be resolved".

Hello Candy,
The code as written declares those fields in the container class, but attempts to reference them in the document class, and that's the problem in a nutshell. Stated another way, the document is trying to directly manipulate the state of the container and that violates the principle of encapsulation.
I am not clear on the relationship between the document and the container classes. In other words I am trying to understand the responsibilites of those two classes (i.e. what they know and what they do in order to solve the overall assignment).
I am curious what your instructor means by having Values visible in the container. There must be some purpose to that, but quite frankly I just don't get it.
The scope of the variables (i.e. the code from which they can be accessed) will depend on where the are declared. Hope this helps.
Regards,
 
wood burning stoves
 
subject: making a class visible
 
Similar Threads
Object behaviour.
assignment problem
History Heuristics in game
Byte Packing/Unpacking
Another recursive problem: choosing login times