-2

If I have a simple List< Point2D > declared.

Example:

List<Point2D> listOfPoints;

/* What I tried */
Point2D point1;
listOfPoints.add(point1);

But, how does one initialize point1 so that I can have a coordinate of let's say (3,2)?

Jackson
  • 9
  • 2
  • Refer to the above link... it will give you all the tools you need to understand this error and fix it for all the future times you're going to have that exception... – Tunaki Oct 05 '16 at 21:33
  • Constructing an object is an extremely basic task. Spend a little time on research before expecting people to spend their time to help you. – shmosel Oct 05 '16 at 21:33

2 Answers2

1

You have to create an instance of Point2D. Right now, you are adding null to your listOfPoints. Plus, listOfPoints is not initialized, so your code would generate a NullPointerException. Try this instead:

List<Point2D> listOfPoints = new ArrayList<>(); // or another List implementation class

Point2D point1 = new Point2D.Float(3, 2); // or perhaps Point2D.Double
listOfPoints.add(point1);

Also, once you have a Point2D.Float or Point2D.Double object, you can set the coordinates explicitly, either by assigning directly to the x and y fields or by calling setLocation() and passing the coordinates.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

You could try:

Point2D point1 = new Point2D.Double(3, 2);

or

Point2D point1 = new Point2D.Float(3, 2);

You will also want to initialise your List, e.g.

List<Point2D> listOfPoints = new ArrayList<>();
listOfPoints.add(point1);

Simply doing new Point2D(3, 2) will not work as Point2D is abstract.

Daniel Squires
  • 338
  • 3
  • 13