Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Inner Class in Java

Thread view: 
Paul Morrison - 09 Jan 2006 17:24 GMT
Hi,

I am trying to create an inner class, its something that Ive never done
before, but I did a Google search and found some information on it, Ive
coded something that I think should work, but I dont know if Ive gone the
right way around it.

Please find the code below. It compiles fine, but I get a
NullPointerException when I do the check for if(point1.getY() <
point2.getY()). For some reason both of the points are null, I know for sure
that the ArrayList poly that I get the Points from are not null, so all I
can think is that it is something to do with the way I have coded the inner
class.

Thanks for your help,

Paul Morrison

*******

public class AETFillAlgorithm extends AbstractScanlineFillAlgorithm {
   ArrayList edgeTable = new ArrayList();

 AETFillAlgorithm(String n, App a, DrawingArea d) {
   super(n, a, d);
 }

 public class Edge{
     Point point1;
     Point point2;

     Edge(Point point1, Point point2) {
         point1 = this.point1;
         point2 = this.point2;
     }

     public Point getPoint1() {
         return point1;
     }

     public Point getPoint2() {
         return point2;
     }

     public ArrayList addEdgeToStore(Edge edge) {
         //get ymin
         ArrayList temp = new ArrayList();
         Point point1 = edge.getPoint1();
         Point point2 = edge.getPoint2();
         if(point1.getY() < point2.getY())   *** This is where the
NullPointerException is thrown.
             temp.add(point1.getY());
         else
             temp.add(point2.getY());

         //get ymax
         if(point1.getY() > point2.getY())
             temp.add(point1.getY());
         else
             temp.add(point2.getY());

         //get xvalue associated with ymin
         if(point1.getY() < point2.getY())
             temp.add(point1.getX());
         else
             temp.add(point2.getX());

         //get 1/m value
         double m = (point1.getY() - point2.getY()) / point1.getX() -
point2.getX();
         temp.add(1/m);

         return temp;
     }
 }

 void fillPoly(Color lineColor, Color fillColor, List<Point> poly) {
     for(int i = 0; i < poly.size() - 1; i++) {
         Point one = (Point) poly.get(i);
         Point two = (Point) poly.get(i+1);
         Edge edge = new Edge(one, two);
         edgeTable.add(edge.addEdgeToStore(edge));
     }
     Edge edge = new Edge(poly.get(0), poly.get(poly.size()-1));
     edgeTable.add(edge.addEdgeToStore(edge));
 }
}
Paul Morrison - 09 Jan 2006 17:27 GMT
Please ignore this post, I have now had it pointed out to me that I have got
the assignment of the point fields the wrong way round!

Paul

> Hi,
>
[quoted text clipped - 83 lines]
>  }
> }
Mark Thomas - 09 Jan 2006 19:09 GMT
> Please ignore this post, I have now had it pointed out to me that I have got
> the assignment of the point fields the wrong way round!
>
> Paul

<snip>

>>         edgeTable.add(edge.addEdgeToStore(edge));
>>     }
>>     Edge edge = new Edge(poly.get(0), poly.get(poly.size()-1));
>>     edgeTable.add(edge.addEdgeToStore(edge));

<snip>

It seems very odd that you should pass edge into edge as a parameter in
your addEdgeToStore method which then only uses the edge passed in and
ignores it's own instance variables.  Surely you don't need that
parameter at all - just use the instance variables?

Mark
Paul Morrison - 09 Jan 2006 19:16 GMT
>>>         edgeTable.add(edge.addEdgeToStore(edge));
>>>     }
[quoted text clipped - 9 lines]
>
> Mark

The reason that I did it this way is because Edge is an inner class and the
method that this is called is outside that class, and I thought it was the
only way that I could get access to the addEdgeToStore method. How do you
suggest that I get round this? I must admit that my Java is slightly rusty!

Paul
Mark Thomas - 10 Jan 2006 09:20 GMT
>>>>        edgeTable.add(edge.addEdgeToStore(edge));
>>>>    }
[quoted text clipped - 11 lines]
>
> The reason that I did it this way is because Edge is an inner class

An inner class is no different from any other class except that it has
access to the private members of its enclosing class.

> and the
> method that this is called is outside that class, and I thought it was the
> only way that I could get access to the addEdgeToStore method. How do you
> suggest that I get round this? I must admit that my Java is slightly rusty!

Sorry Paul, I thought my message told you how.  Here it is in more
detail.  Change your addEdgeToStoreMethod to leave out the parameter:

      public ArrayList addEdgeToStore() {    // changes here
          //get ymin
          ArrayList temp = new ArrayList();
          Point point1 = getPoint1();        // changes here
          Point point2 = getPoint2();        // changes here
          if(point1.getY() < point2.getY())
              temp.add(point1.getY());
          else
              temp.add(point2.getY());

          //get ymax
          if(point1.getY() > point2.getY())
              temp.add(point1.getY());
          else
              temp.add(point2.getY());

          //get xvalue associated with ymin
          if(point1.getY() < point2.getY())
              temp.add(point1.getX());
          else
              temp.add(point2.getX());

          //get 1/m value
          double m = (point1.getY() - point2.getY()) / point1.getX() -
point2.getX();
          temp.add(1/m);

          return temp;
      }
  }

then change the calls to that method in your fillPoly method:

  void fillPoly(Color lineColor, Color fillColor, List<Point> poly) {
      for(int i = 0; i < poly.size() - 1; i++) {
          Point one = (Point) poly.get(i);
          Point two = (Point) poly.get(i+1);
          Edge edge = new Edge(one, two);
          edgeTable.add(edge.addEdgeToStore());        // changes here
      }
      Edge edge = new Edge(poly.get(0), poly.get(poly.size()-1));
      edgeTable.add(edge.addEdgeToStore());        // changes here
  }

I haven't fixed your original problem, just simplified the method as
suggested.

Mark
Ricky Clarkson - 13 Jan 2006 01:32 GMT
If you get into the habit of making method and constructor parameters
final, then you won't be able to make this mistake.  Well, the compiler
won't let you..

public void makeAMistake(final int x)
{
  x=y; //should have been y=x, but I made a mistake
}

The compiler will complain.
Roedy Green - 09 Jan 2006 18:08 GMT
>     Point point1 = edge.getPoint1();
>          Point point2 = edge.getPoint2();
>          if(point1.getY() < point2.getY())   *** This is where the

Dump things out .is point1 point2 null?  is edge null?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.