-1

I have created a Point class which creates a 2D point and a Segment class which inherits from the Point class.I wanted to test and print the length of the segment,but it gives null pointer exception.

Point.class

    public class Point {
            protected double x;
        protected double y;

        public Point(double x,double y){
                this.x=x;
            this.y=y;
        }
        public double getX(){return this.x;}
        public double getY(){return this.y;}
        public double distance(){
            double dist=Math.sqrt(this.x * this.x+this.y * this.y);
            return dist;
        }
        public double distance(Point point){
            double dx=this.x-point.x;
                double dy=this.y-point.y;
            double dis=Math.sqrt(dx*dx+dy*dy);
            return dis;
        }

        void translate(double dx,double dy){
            this.x=dx+this.x;
            this.y=dy+this.y;

        }

        public static Point barycenter(Point[] points){
            float bx=(float) ((points[0].x+points[1].x)/2);
            float by=(float)(points[0].y+points[1].y)/2;
            Point b=new Point(bx,by);
            return b; 
        }
    }

Segment.class

    public class Segment extends Point{
        public Segment(double x, double y) {
           super(x, y);
        }
        Point p1;
        Point p2;

        public void set1(double a,double b) {
            p1.x=a;
            p1.y=b;
        }
        public void set2(double a,double b) {
                p2.x=a;
            p2.y=b;
        }

        public double getx1() {
            return p1.x;
        }
        public double getx2() {
            return p2.x;
        }
        public double gety1() {
            return p1.y;
        }
        public double gety2() {
            return p2.y;
        }
        public double length() {
            double l=Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
            return l;
        }
       }

Main.class

    public class Main {
        public static void main(String[] args) {
            Point a1=new Point(4.0, 8.9);
            Point a2=new Point(7.3, 6.0);
            Segment ab=new Segment(3.0,4.0);
            System.out.println(ab.length());
        }
    }

This is the output i am getting

   Exception in thread "main" java.lang.NullPointerException
       at Segment.length(Segment.java:31)
       at Main.main(Main.java:5)

When in fact i would like to get a different answer. How do i get rid of this problem?

ichigo14
  • 63
  • 5

2 Answers2

2

You defined the methods set1 and set2 but never used them. (Maybe Segment shouldn't be a subclass of Point, or at least should have a constructor that takes 2 Points?)

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

It is giving you a NullPointerException because you're creating a new Segment but the constructor doesn't update the p1, and p2 Point objects.

You need to update the constructor in Segment to include 2 Point objects for p1 and p2.

It should also noted that Segment should be composed of Point objects, not inherit them. This allows the system to be open for extension. Also, I would suggest that the fields be final to promote immutability, improving thread safety.

public Segment(Point p1, Point p2) {
    this.p1 = p1;
    this.p2 = p2;
}
        public static void main(String[] args) {
            Point a1 = new Point(4.0, 8.9);

            Point a2 = new Point(7.3, 6.0);

            Segment ab= new Segment(a1, a2);

            System.out.println(ab.length());
        }
Jason
  • 5,154
  • 2
  • 12
  • 22