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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Passing An Object To The Constructor Of Another Object

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile 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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;
    	}

Similar Threads

  1. [SOLVED] Java constructor/object
    By maple1100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 21st, 2013, 03:14 PM
  2. Passing as a parameter, an object + another variable
    By goochasauras in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2012, 10:13 PM
  3. passing object and storing
    By jack_nutt in forum What's Wrong With My Code?
    Replies: 15
    Last Post: June 30th, 2011, 09:32 PM
  4. Passing reference via object
    By Stefan_Lam in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2011, 10:57 AM
  5. Object array with constructor
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2010, 11:02 AM