Results 1 to 10 of 10
- 11-30-2009, 05:07 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
[UPDATED W/ CODE]A few general questions regarding scope of local inner classes.
Add the code for anyone who has a second to look it over....
--------------------------------------------------------
I am trying to put together a method with the following characteristics and behaviors:
-no parameters
-works on two types, the class in which the method is defined, and a direct subclass created within the method itself.
-modify the calling type into the subclass type.
-differentiate between which type (base or subclass) it was called from.
That's very general, so here are a few specific questions.
-Where all is it possible for a direct subclass of type A, defined as a local inner class (inside a method defined in A) exist?
-How strict are rules defining scope for local inner classes? Is anyone aware of a definitive reference available? I've done plenty of reading, but still find myself using trial and error.
If anyone is interested, I can give the code, but don't mind speaking in more general terms. I'm aware that what I'm attempting is beyond the boundaries of ration method functionality, and more so bad OO, this is simply me going above and beyond on an assignment with strict guidelines and pushing it to the limit.
Thanks in advance for any guidance!!Last edited by Half_Duplex; 12-01-2009 at 08:17 AM.
-
Speaking for myself, I'd like to see a simple code example of what you're asking about.
- 11-30-2009, 06:52 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
I scrapped the code about 5 minutes ago, gone for good. With the amount of errors and the tweaking time it would take.... I would rather just spend the time addressing the fundamental misunderstanding that created them in the first place.
What's needed basically is a method with no params that encrypts and decrypts and image file. The parent class is given which also limits the possibilities.
What I attempted to do was create multiple local inner classes inside the method;
-class representing a pixel.
-class representing an encrypted picture (extension of methods parent class) that included an ArrayList of pixel objects.
-Comparator classes for encryption and decryption that would sort the pixel object array.
-various inner methods that would either sort an encrypted objects array, or create an encrypted object with an array from an unencrypted object.
The problem was all in the scope of the local classes. I feel that what I was trying to do is just not possible, even with a massive amount of code.
Any thoughts?
- 11-30-2009, 09:36 PM #4
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
It sounds like you had concerns about the data structure
you devised within a subclass, or possibly you are adding
to the source code of some other class.
If your concerns are about your data structure which you
need to analyze the pixel data, the general rule is:
A referenced data structure will continue to exist as long
as at least one reference exists that points to it.
Sometimes programmers forget to set a reference to
null, unintentionally allowing that structure to exist
beyond their intentions. The risk is that it may grow
and grow, resulting in a memory leak.
If your reference is within a method, your data will be
destroyed when execution exits that method UNLESS,
somewhere in your method's code, you assigned that
reference to another reference outside of the method.
- 12-01-2009, 02:57 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
The data structure existence was something I took into account in planning, and yes I am modifying a pre existing class with restrictions on what I can do.
The issues I had were related more to scope. I needed multiple comparators, multiple new local classes, inheritance between classes, parameters not being available, interface implementations, qualified names..... it became a maze basically!!
- 12-01-2009, 08:16 AM #6
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
Here's a snip of what I've done so far, basically just trying to retrofit the classes.Java Code:public static class EPicture extends Picture{ int height, width, total; PixelNode[] pixelNodeArray; Picture uPicture; public EPicture(Picture pic){ uPicture = new Picture(pic); height = uPicture.getHeight(); width = uPicture.getWidth(); total = height*width; pixelNodeArray = new PixelNode[total]; } public void construct(){ int i = 0; int w = 0; int h = 0; int setRGB = pixelNodeArray[i].RGB; for (; w<width; w++){ for (; h<height; h++, i++){ setBasicPixel(w, h, setRGB); } } } public class PixelNode implements Comparable<PixelNode>{ int RGB; Integer oI; Double avg; Pixel pixel; public int compareTo(PixelNode pN){ return oI.compareTo(pN.oI); } public Double getAverage(){ return avg; } } [U] [B][I][I]public static class Encrypt implements Comparator<PixelNode>{[/I][/I][/B][/U] public int compare(PixelNode pN1, PixelNode pN2){ [I][U] [B]int compare = pN2.getAverage().compareTo(pN1.getAverage());[/B][/U][/I] return compare; } public boolean equals(PixelNode pN){ return false; } } } public void obscure(){ if(this instanceof Picture){ EPicture origPic = new EPicture(this); for(int w=0;w>origPic.getWidth();w++){ int i = 0; for(int h=0;h>origPic.getHeight();h++, i++){ origPic.pixelNodeArray[i].pixel = new Pixel(origPic, w, h); origPic.pixelNodeArray[i].oI = i; origPic.pixelNodeArray[i].RGB = origPic.getBasicPixel(w,h); origPic.pixelNodeArray[i].avg = origPic.pixelNodeArray[i].pixel.getAverage(); } } EPicture.Encrypt encryptionSort = new EPicture.Encrypt(); Arrays.sort(origPic.pixelNodeArray, encryptionSort); origPic.construct(); origPic.show(); } }
I'm getting null pointer exceptions at the two indicated lines and about 30 at java.util.Arrays.mergeSort(Unknown Source).
Fairly new at debugging, but I'm sure this problem is originating from the fact that the comparator definition is in a two deep class. I've tried a few different solutions such as using a non static Encrypt and creating an instance in EPicture() and so on, can't figure it out.
-
You've got null values in your origPic.pixelNodeArray. Either somehow remove those null values or allow your comparator's compare method to be able to handle nulls before the method attempts to dereference either PixelNode parameter.
- 12-01-2009, 02:36 PM #8
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
I see, I'll take a look into that. To be honest, not as much worried about fixing it as much as finding out what was done wrong.
-
If your main error now is the NPE, then this is what is wrong. Else are there other errors that you're seeing?
As an aside, I'm not sure I'd try to do so much with inheritance but would look into perhaps a solution using composition or even wrapper classes.
- 12-01-2009, 08:30 PM #10
Member
- Join Date
- Nov 2009
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Flicker with resizing and need general code examination
By MrFreeweed in forum Java 2DReplies: 4Last Post: 01-13-2011, 05:30 PM -
general confused about java question (easy!)
By sweetjava in forum New To JavaReplies: 1Last Post: 08-09-2009, 02:03 AM -
Issue with TreeViewer and JPopupMenu and components in general
By xcallmejudasx in forum Advanced JavaReplies: 1Last Post: 11-13-2008, 11:43 PM -
General Discussion on Abstract
By sanjeevtarar in forum Advanced JavaReplies: 15Last Post: 05-06-2008, 06:16 AM -
How to set General options in NetBeans IDE
By JavaForums in forum NetBeansReplies: 0Last Post: 08-02-2007, 12:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks