# ArrayList for 2DPoints

• January 6th, 2011, 09:41 PM
bengregg
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...

Code java:

```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.
• January 6th, 2011, 09:56 PM
copeg
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.