Results 1 to 10 of 10
  1. #1
    Half_Duplex is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default [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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Half_Duplex is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    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?

  4. #4
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    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.

  5. #5
    Half_Duplex is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    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!!

  6. #6
    Half_Duplex is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    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();
        }
      }
    Here's a snip of what I've done so far, basically just trying to retrofit the classes.

    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  8. #8
    Half_Duplex is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    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.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  10. #10
    Half_Duplex is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    That's the problem, I was given strict guidelines.....

    The code is scrapped though, what I'm attempting just isn't very feasible.

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2011, 05:30 PM
  2. general confused about java question (easy!)
    By sweetjava in forum New To Java
    Replies: 1
    Last Post: 08-09-2009, 02:03 AM
  3. Issue with TreeViewer and JPopupMenu and components in general
    By xcallmejudasx in forum Advanced Java
    Replies: 1
    Last Post: 11-13-2008, 11:43 PM
  4. General Discussion on Abstract
    By sanjeevtarar in forum Advanced Java
    Replies: 15
    Last Post: 05-06-2008, 06:16 AM
  5. How to set General options in NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 08-02-2007, 12:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •