-2

The Program I'm trying to make is supposed to draw a Polygon using points stored in an ArrayList. I rechecked the code and I am also using the driver, but when I try to run the program, it gives me a NullPointerException when I try to add coordinates for the program to use when drawing.

import java.awt.geom.*;
import java.util.ArrayList;
import gpdraw.*;
public class IrregularPolygon
{
    private ArrayList <Point2D.Double> myPolygon;
    DrawingTool myPen;
    SketchPad myPaper; 
    double dblPerimeter;
    double dblSegment;
    double dblArea;

    public IrregularPolygon()//Creating the sketchpad and pen
    {
        myPaper = new SketchPad(500,500);
        myPen = new DrawingTool(myPaper);
    }

    public void add(Point2D.Double aPoint)//Adds to the array
    {
        myPolygon.add(aPoint);// This is where the problem is I think, I don't know what to do
    }

    public void draw()//This method draws the polygon
    {
        for(int x = 0; x < myPolygon.size() - 1 ; x++)
        {
            myPen.down();
            myPen.move(myPolygon.get(x).getX(),myPolygon.get(x).getY());
            myPen.up();
        }   
    } 

    public double perimeter()//This method finds the perimeter
    {
        for(int x = 0; x < myPolygon.size() - 1; x++)
        {
            dblPerimeter += myPolygon.get(x).distance(myPolygon.get(x+1));
        }
        return dblPerimeter;
    }

    public double area()//This method finds the area
    {
        for(int x = 0; x < myPolygon.size() - 1; x++)
        {
            double X1 = (myPolygon.get(x).getX());
            double Y1 = (myPolygon.get(x).getY());
            double X2 = (myPolygon.get(x + 1).getX());
            double Y2 = (myPolygon.get(x + 1).getY());
            dblArea += (X1 * Y2 - Y1 * X2);

        }
        return dblArea;
    }
}

and the programs runs from this driver:

import java.util.*;
import java.awt.geom.Point2D;
public class IrregularPolygonDriver
{
    static boolean blnReadyToDraw = false;
    static Scanner in = new Scanner(System.in);
    public static void main(String[]args)
    {
        int num;
        IrregularPolygon poly = new IrregularPolygon();
        while(blnReadyToDraw == false)
        {
            System.out.println("Would you like to add a point, find the perimeter, or calculate the area?");
            System.out.println("To add a point, enter 1; to find the perimeter, enter 2; to find the area, enter 3");
            num = in.nextInt();
            if(num == 1)
            {
                double dblNum1;
                double dblNum2;
                Point2D.Double dblNumber;
                System.out.println("Enter an x value and a y value: ");
                dblNum1 = in.nextDouble();
                dblNum2 = in.nextDouble();
                dblNumber = new Point2D.Double(dblNum1,dblNum2);
                poly.add(dblNumber);
                System.out.println("Keep adding points until you are done drawing the figure.");
            }
            else if(num == 2)
            {
                poly.perimeter();
            }
            else if(num == 3)
            {
                poly.area();
            }
            else
            {
                blnReadyToDraw = true;
                System.out.println("Preparing to Draw");
            }
        }
        poly.draw();
    }  
}
Todd
  • 30,472
  • 11
  • 81
  • 89
Zak
  • 43
  • 2
  • 8

2 Answers2

2

This is happening because your myPolygon is null.

Change:

private ArrayList <Point2D.Double> myPolygon;

To this:

private ArrayList <Point2D.Double> myPolygon = new ArrayList<>();

Or, in your constructor, add this line:

myPolygon = new ArrayList<>();
Todd
  • 30,472
  • 11
  • 81
  • 89
2

You didn't initialize your ArrayList,

private ArrayList <Point2D.Double> myPolygon;

is equivalent to

private ArrayList <Point2D.Double> myPolygon = null;

so when you say

myPolygon.add(aPoint); // <-- null.add(aPoint);

You could assign it in the constructor, or at declaration (using the List interface and the diamond operator <>) with something like

private List<Point2D.Double> myPolygon = new ArrayList<>();
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249