This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

making a class visible

 
Candy Bortniker
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Candy Bortniker
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Candy Bortniker
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Howard Kushner
author
Ranch Hand
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Candy Bortniker
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also LimitedTextTest that is the driver for this whole thing. Would you like to see that too?
 
Howard Kushner
author
Ranch Hand
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Howard Kushner
author
Ranch Hand
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic