Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ArrayList for 2DPoints

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList for 2DPoints

    I just started my second quarter of Java. I'm in Java 2 now anyways I was given this as my first assignment:

    Implement a Class Polygon that contains an array List of Point2D.Double objects. Support methods

    public void add(Point2D.Double aPoint)
    public void draw(Graphics g2)

    Draw the polygon by joining adjacent points with a line, and then closing it up by joining the end and start points.
    Write a graphical application that draws a square and a pentagon using two Polygon Objects.

    I don't have much experience with ArrayLists so I'm pretty lost but this is what I have so far...

    private ArrayList<Point2D.Double> myPolygon;
     
    public void add(Point2D.Double aPoint) {
    System.out.println(aPoint); // Outputs Point2D.Double[20.0, 10.0]
    myPolygon.add(aPoint); // NullPointerException gets thrown here
    }
     
    and for my main class:
     
    Polygon poly = new Polygon();
     
    Point2D.Double a = new Point2D.Double(20,10);
    poly.add(a);
     
    Point2D.Double b = new Point2D.Double(30,50);
    poly.add(b);
     
    Point2D.Double c = new Point2D.Double(40,60);
    poly.add(c);
     
    Point2D.Double d = new Point2D.Double(70,90);
    poly.add(d);
     
     
    Line2D.Double segmenta = new Line2D.Double(a, b);
    Line2D.Double segmentb = new Line2D.Double(b, c);
    Line2D.Double segmentc = new Line2D.Double(c, d);
    Line2D.Double segmentd = new Line2D.Double(d, a);
    I'm pretty lost so any help is appreciated. I'm still waiting on my book to arrive so I don't have much to work from right now.
    Last edited by copeg; January 6th, 2011 at 09:53 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ArrayList for 2DPoints

    For future reference, please flank your code with the [highlight=java][/highlight] tags. I have edited your post above to reflect this.

    Your code mentions a NullPointerException, this is thrown because the variable myPolygon is never initialized (see Common Java Mistakes). Is that the only problem? If not, please define what other problems you are having, and if there are exceptions please post them in their entirety.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  3. ARRAYLIST
    By xs4rdx in forum Collections and Generics
    Replies: 4
    Last Post: March 25th, 2010, 10:36 AM
  4. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM