我想创建一个三角形对象并获取三角形的面积和周长。我在NullPointerException
和Triangle.area
获得Demo.main
。似乎没有值传递给区域方法和周边方法,但我无法看到我的代码出错的地方。这是我的代码。
//POINTCLASS
public class Point {
private int x;
private int y;
public Point(int X, int Y){
this.x = X;
this.y = Y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public double distance(Point p){
double distance,dx,dy;
dx=x-p.x;
dy=y-p.y;
distance = Math.sqrt((dx*dx)+(dy*dy));
return distance;
}
public void translate(int dx, int dy){
x+=dx;
y+=dy;
}
public void scale(int factor){
x*=factor;
y*=factor;
}
public String toString(){
String attributes = "x = " + x + "," + " y = " + y;
return attributes;
}
}
//TRIANGL CLASS
public class Triangle {
private int sides = 3;
private Point point1,point2,point3;
private Point[] Points;
public Triangle(Point[] vertices){
Points = vertices;
}
public Triangle(int x1,int y1,int x2,int y2,int x3, int y3){
point1 = new Point(x1,y1);
point2 = new Point(x2,y2);
point3 = new Point(x3,y3);
Points[0]=point1;
Points[1]=point2;
Points[2]=point3;
}
public double perimeter(){
double side1=point1.distance(point2);
double side2=point2.distance(point3);
double side3=point1.distance(point3);
return side1+side2+side3;
}
//issue with code seems to be here
public double area(){
double side1=point1.distance(point2);
double side2=point2.distance(point3);
double side3=point1.distance(point3);
double s = perimeter()/2;
double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}
public String toString(){
String attributes = "Sides " + sides;
return attributes;
}
public void translate(int dx, int dy){
Points[0].translate(dx,dy);
Points[1].translate(dx, dy);
Points[2].translate(dx, dy);
}
public void scale(int factor){
vertices*=factor;
}
public Point getVertex(int i){
return vertices[i];
}
}
//MAIN CLASS
public class GDemo {
public static void main(String[] args) {
Point p1 = new Point(0,20);
Point p2 = new Point(20,0);
Point p3 = new Point(30,30);
Point[] vertices = {p1,p2,p3};
Triangle t = new Triangle(vertices);
System.out.println("Triangle" + t.toString() + " area:" + t.area() +
" perimeter: " + t.perimeter());
t.translate(10, 15);
System.out.println("Triangle" + t.toString() + " area:" + t.area() +
" perimeter: " + t.perimeter());
t.scale(2);
System.out.println("Triangle" + t.toString() + " area:" + t.area() +
"perimeter: " + t.perimeter());
}
}
答案 0 :(得分:0)
您调用构造函数public Triangle(Point[] vertices)
并且未设置point1,point2和point3值 - 它们保持为null。因为这样,当你在方法区域中尝试计算双side1 = point1.distance(point2);
它会抛出NPE。
答案 1 :(得分:0)
在创建Triangle(Point[] vertices)
对象时,您正在调用构造函数Triangle
。因此,您的这些字段仍然未初始化private Point point1,point2,point3;
这就是您获得NullPointerException
的原因。
固定构造函数应该是这样的:
public Triangle(Point[] vertices){
Points = vertices;
// initialize point1, point2, point3 here.
if(vertices.length > 2){
this.point1 = vertices[0];
this.point2 = vertices[1];
this.point3 = vertices[2];
}
}
答案 2 :(得分:-4)
尝试将整个代码放入try-catch块并捕获 NullPointerException 并编写
e.printStackTrace();
例如:
try{
//your block of code
}catch(NullPointerException e){
e.printStackTrace();
}
它将显示错误代码的行号