Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: Passing An Object To The Constructor Of Another Object

1. 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
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
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
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!  Reply With Quote

2. Re: Passing An Object To The Constructor Of Another Object

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.  Reply With Quote

3. 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 :

public PlaneCircle(Point p, double r){
super(r);
newPoint =p;
}  Reply With Quote