# Passing An Object To The Constructor Of Another Object

• December 1st, 2013, 12:08 PM
mynorka
Passing An Object To The Constructor Of Another Object
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
Code :

```public 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); } }```
PlaneCircle
Code :

```public 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); } }```
Main
Code :

``` 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);```
Also, each class is a part of the same package.

Thanks!
• December 1st, 2013, 12:22 PM
Norm
Re: Passing An Object To The Constructor Of Another Object
Quote:

I get a NullPointerException error.
Please copy the full text of the error message and paste it here. It has important info about the error.
• December 2nd, 2013, 03:24 PM
MalikDz
Re: Passing An Object To The Constructor Of Another Object
"newPoint" default value is null , you did not instance it no where .When you create an object it's call the contrustor automaticaly. You did not assign any value to newPoint in the constructor. So basicly you're trying to assign a value to your object field when the object is null. This is the reason why its throwing an NPE.

Here what you could do :

Code :

``` public PlaneCircle(Point p, double r){ super(r); newPoint =p; }```