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.