-1

Intermediate Programmer here. Wondering what in my code is causing a NullPointerException in the Trapezoid Class.

Trapezoid is intended to be able to create the characteristics of a trapezoid using both left-side points and the top and bottom lines of a trapezoid as input. The top and bottom lines of the trapezoid are straight.

Point class:

    class Point {
        double pointx = 0;
        double pointy = 0;
    public Point(double x, double y) {
        pointx = x;
        pointy = y;
    }
    public double getX() {
        double getx = pointx;
        return getx;
    }
    public double getY() {
        double gety = pointy;
        return gety;
    }

trapezoid class:

    class Trapezoid extends Shape {
        Point tl;
        Point bl;
        Point tr;
        Point br;
        double topl;
        double botl;
        double leftl;
        double rightl;
        public Trapezoid(Point topLeftPoint, Point bottomLeftPoint, double topSide, double bottomSide) {
            topl = topSide;
            botl = bottomSide;
            leftl = Math.sqrt(Math.pow((topLeftPoint.getX()-
    bottomLeftPoint.getX()),2)+Math.pow((topLeftPoint.getY()-
    bottomLeftPoint.getY()),2));
            tr.pointx = topLeftPoint.pointx+topl;
            tr.pointy = topLeftPoint.pointy;
            br.pointx = bottomLeftPoint.pointx+botl;
            br.pointy = bottomLeftPoint.pointy;
            rightl = Math.sqrt(Math.pow((tr.getX()-
    br.getX()),2)+Math.pow((tr.getY()-br.getY()),2));
        }
        public Point getTopLeftPoint() {
            return tl;
        }
        public Point getBottomLeftPoint() {
            return bl;
        }
        public double getTopSide() {
            return topl;
        }
        public double getBottomSide() {
            return botl;
        }
        public double getArea() {
            double area;
            area = ((topl+botl)/2)*(tl.pointy-bl.pointy);
            return area;
        }
        public double getPerimeter() {
            double perimeter = topl+botl+leftl+rightl;
            return perimeter;
        }
    }

In particular, I am getting the NullPointerException here:

    tr.pointx = topLeftPoint.pointx+topl;
    tr.pointy = topLeftPoint.pointy;
    br.pointx = bottomLeftPoint.pointx+botl;
    br.pointy = bottomLeftPoint.pointy;

Any help you are able to provide would be helpful. I am probably overlooking something very simple, but I can't seem to understand what that is.

  • 1
    Pro tip: use a debugger and step through your code. – forgivenson Nov 20 '17 at 02:57
  • You need to call `new Point(x, y)` to **create** `Point` instances, you can't access the members of a `null` instance through a `Point` reference. – Elliott Frisch Nov 20 '17 at 03:00
  • The heuristic for debugging a NullPointerException is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Nov 20 '17 at 03:02

1 Answers1

0

tr and br are never initialized, so they are equal to null when you try to use them at tr.pointx ..., etc. You need to create those points before using them.

forgivenson
  • 4,394
  • 2
  • 19
  • 28