Hello all! This is my first post in this forum, and it's about objects.
I have two separate classes, Point and PlaneCircle. The Point class has two fields, x and y, which are used to construct a coordinate pair on a plane. The PlaneCircle class is supposed to use the coordinates from a Point object, along with a radius, to construct a circle on a plane.
I have code for both Point and PlaneCircle. When trying to create new instances of PlaneCircle using previous instances of Point, I get a NullPointerException error. The code is below.
Point Class
PlaneCirclepublic class Point implements Cloneable{ public double x, y; public Point(double x, double y){ this.x = x; this.y = y; } public void setX(double x) { this.x = x; } public double distanceFromOrigin(){ return Math.sqrt(x*x + y*y); } }
Mainpublic class PlaneCircle extends Circle { Point newPoint; public PlaneCircle(Point p, double r){ super(r); newPoint.x = p.x; newPoint.y = p.y; } public boolean isInside(double x, double y){ double dx = x - newPoint.x, dy = y - newPoint.y; double distance = Math.sqrt(dx*dx + dy*dy); return (distance > r); } }
Also, each class is a part of the same package.public static void main(String[] args) { Point po1 = new Point(10, 10); Point po2 = new Point(11, 11); Point po3 = new Point(12, 12); Point po4 = new Point(13, 13); PlaneCircle pcr1 = new PlaneCircle(po1, 11f); PlaneCircle pcr2 = new PlaneCircle(po2, 22f); PlaneCircle pcr3 = new PlaneCircle(po3, 33f); PlaneCircle pcr4 = new PlaneCircle(po4, 44f);
Thanks!