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?