NullPointerException,以递归方式划分三角形

时间:2018-10-12 05:18:22

标签: java recursion nullpointerexception

我在创建的boolean divide(int depth)类中的Triangle方法遇到问题,代码如下。其他部分已经通过了测试,但是由于boolean divide(int depth),此编程引发了java.lang.NullPointerException

我已经研究了很长时间了,但是我仍然不知道递归有什么问题。 boolean divide()boolean divide(int depth)都不接受任何参数,这使我很难编写递归。任何提示将不胜感激。

boolean divide(int depth)覆盖boolean divide()boolean divide()将三角形划分为三个子三角形,在三角形的每个边上创建三个Vertex2D,子三角形存储在数组中的Triangle类作为属性。

public class Triangle {
    private static final int NUM_OF_SUB_TRIANGLES = 3;
    private final Vertex2D[] vertices = new Vertex2D[3];
    private final Triangle[] subTriangles = new 
Triangle[NUM_OF_SUB_TRIANGLES];
    public  Triangle(Vertex2D v1, Vertex2D v2, Vertex2D v3){
        vertices[0] = v1;
        vertices[1] = v2;
        vertices[2] = v3;
    }

// Method returns a vertex of the triangle according to input index
public Vertex2D getVertex(int index) {
    for (int i = 0; i < 3; i++) {
        if (index == i) {
            return vertices[i];
        }
    }
    return null;
}

public boolean isEquilateral(){
    double side1 = vertices[0].distance(vertices[1]);
    double side2 = vertices[0].distance(vertices[2]);
    double side3 = vertices[1].distance(vertices[2]);

    if(Math.abs(side1-side2) < 0.001 && Math.abs(side1-side3) < 0.001 && Math.abs(side2-side3) < 0.001){
        return true;
    }else{
        return false;
    }

}

// Method outputs the coordinates of threes vertices of the triangle
// edit this one
public String toString(){
    String s = "Triangle: vertices=" + vertices[0]
            + " [" + vertices[1].getX() + ", " + vertices[1].getY() + "] " + "[" + vertices[2].getX() + ", " + vertices[2].getY()
            + "]";
    return s;
}

// Method checks if the three sub triangles are created
public boolean isDivided(){
    for(int i = 0 ; i < NUM_OF_SUB_TRIANGLES; i ++){
        if(subTriangles[i] == null){
            return false;
        }
    }
    return true;
}

// Method returns a sub triangle according to the input index
public Triangle getSubTriangle(int index){
    for (int i = 0; i < 3; i++) {
        if (index == i) {
            return subTriangles[i];
        }
    }
    return null;
}

// Method divides the triangle into three sub triangles

public boolean divide(){
    if (isDivided()){
        return false;
    }else{
        // Find vertices of the three sub triangles
        Triangle t1 = new Triangle(vertices[0], vertices[0].createMiddle(vertices[1]), vertices[0].createMiddle(vertices[2]));
        Triangle t2 = new Triangle(vertices[0].createMiddle(vertices[1]), vertices[1], vertices[1].createMiddle(vertices[2]));
        Triangle t3 = new Triangle(vertices[0].createMiddle(vertices[2]), vertices[1].createMiddle(vertices[2]), vertices[2]);

        subTriangles[0] = t1;
        subTriangles[1] = t2;
        subTriangles[2] = t3;

        return true;
    }
}

public boolean divide(int depth){
    if(depth <= 0){
        return false;
    }else{
        while(depth > 0) {
            if (!isDivided()) {
                divide();
            } else {
                depth -= 1;
                for (int i = 0; i < 3; i++) {
                    getSubTriangle(i).divide(depth);
                }
                depth -= 1;
            }
        }
        return true;
    }

}
}

2 个答案:

答案 0 :(得分:0)

问题似乎出在此代码上

        char[] input = { 'h', 'e', 'l', 'l', 'o' };
        char[] inputnew = new char[input.Length];
        Array.Copy(input, inputnew, input.Length);
        Array.Reverse(inputnew);
        Console.WriteLine(new string(input));
        Console.WriteLine(new string(inputnew));

但是为什么您甚至使用这种方法循环

public Triangle getSubTriangle(int index){
    for (int i = 0; i < 3; i++) {
        if (index == i) {
            return subTriangles[i];
        }
    }
    return null;    // HERE
}

,然后检查您的呼叫代码,重调的值为空

答案 1 :(得分:0)

问题出在getSubTriangle(i).divide(depth)上。更改您的代码:

public boolean divide(int depth){
    if(depth <= 0){
        return false;
    }else{
        while(depth > 0) {
            if (!isDivided()) {
                divide();
            } else {
                depth -= 1;
                for (int i = 0; i < 3; i++) {
                    Triangle t=getSubTriangle(i);
                    if (t!=null) {
                      t.divide(depth);
                    }
                }
                depth -= 1;
            }
        }
        return true;
    }

}

只需对其进行测试:

public class Triangle {

    public static class Vertex2D {

        private float x;
        public Vertex2D(float i, float j) {
            x=i;
            y=j;
        }



        public float getX() {
            return x;
        }



        public void setX(float x) {
            this.x = x;
        }



        public float getY() {
            return y;
        }



        public void setY(float y) {
            this.y = y;
        }



        private float y;

        public double distance(Vertex2D vertex2d) {
            float x1=vertex2d.x-x;
            float y1=vertex2d.y-y;

            return Math.sqrt(x1*x1+y1*y1);
        }



        public Vertex2D createMiddle(Vertex2D vertex2d) {
            float x1=vertex2d.x+x;
            float y1=vertex2d.y+y;

            Vertex2D v=new Vertex2D(x1, y1);
            v.setX(x1);
            v.setY(y1);


            return v;
        }

    }

    private static final int NUM_OF_SUB_TRIANGLES = 3;
    private final Vertex2D[] vertices = new Vertex2D[3];
    private final Triangle[] subTriangles = new 
Triangle[NUM_OF_SUB_TRIANGLES];
    public  Triangle(Vertex2D v1, Vertex2D v2, Vertex2D v3){
        vertices[0] = v1;
        vertices[1] = v2;
        vertices[2] = v3;
    }

// Method returns a vertex of the triangle according to input index
public Vertex2D getVertex(int index) {
    for (int i = 0; i < 3; i++) {
        if (index == i) {
            return vertices[i];
        }
    }
    return null;
}

public boolean isEquilateral(){
    double side1 = vertices[0].distance(vertices[1]);
    double side2 = vertices[0].distance(vertices[2]);
    double side3 = vertices[1].distance(vertices[2]);

    if(Math.abs(side1-side2) < 0.001 && Math.abs(side1-side3) < 0.001 && Math.abs(side2-side3) < 0.001){
        return true;
    }else{
        return false;
    }

}

// Method outputs the coordinates of threes vertices of the triangle
// edit this one
public String toString(){
    String s = "Triangle: vertices=" + vertices[0]
            + " [" + vertices[1].getX() + ", " + vertices[1].getY() + "] " + "[" + vertices[2].getX() + ", " + vertices[2].getY()
            + "]";
    return s;
}

// Method checks if the three sub triangles are created
public boolean isDivided(){
    for(int i = 0 ; i < NUM_OF_SUB_TRIANGLES; i ++){
        if(subTriangles[i] == null){
            return false;
        }
    }
    return true;
}

// Method returns a sub triangle according to the input index
public Triangle getSubTriangle(int index){
    for (int i = 0; i < 3; i++) {
        if (index == i) {
            return subTriangles[i];
        }
    }
    return null;
}

public static void main(String args[]) {
    Vertex2D v1=new Vertex2D(10,20);
    Vertex2D v2=new Vertex2D(-10,-20);
    Vertex2D v3=new Vertex2D(10,-20);
    Triangle t=new Triangle(v1, v2, v3);

    boolean r = t.divide(3);
    System.out.println(r);

}

// Method divides the triangle into three sub triangles

public boolean divide(){
    if (isDivided()){
        return false;
    }else{
        // Find vertices of the three sub triangles
        Triangle t1 = new Triangle(vertices[0], vertices[0].createMiddle(vertices[1]), vertices[0].createMiddle(vertices[2]));
        Triangle t2 = new Triangle(vertices[0].createMiddle(vertices[1]), vertices[1], vertices[1].createMiddle(vertices[2]));
        Triangle t3 = new Triangle(vertices[0].createMiddle(vertices[2]), vertices[1].createMiddle(vertices[2]), vertices[2]);

        subTriangles[0] = t1;
        subTriangles[1] = t2;
        subTriangles[2] = t3;

        return true;
    }
}

public boolean divide(int depth){
    if(depth <= 0){
        return false;
    }else{
        while(depth > 0) {
            if (!isDivided()) {
                divide();
            } else {
                depth -= 1;
                for (int i = 0; i < 3; i++) {
                    Triangle t = getSubTriangle(i);
                    System.out.println(t.toString());
                            if (t!=null) {
                                t.divide(depth);
                            }
                }
                depth -= 1;
            }
        }
        return true;
    }

}
}

输出为(我不检查算法结果的含义):

Triangle: vertices=Triangle$Vertex2D@15db9742 [0.0, 0.0] [20.0, 0.0]
Triangle: vertices=Triangle$Vertex2D@15db9742 [10.0, 20.0] [30.0, 20.0]
Triangle: vertices=Triangle$Vertex2D@15db9742 [20.0, 40.0] [40.0, 40.0]
Triangle: vertices=Triangle$Vertex2D@6d06d69c [10.0, 20.0] [40.0, 40.0]
Triangle: vertices=Triangle$Vertex2D@7852e922 [40.0, 40.0] [30.0, 20.0]
Triangle: vertices=Triangle$Vertex2D@4e25154f [0.0, 0.0] [20.0, 0.0]
Triangle: vertices=Triangle$Vertex2D@4e25154f [10.0, 20.0] [30.0, 20.0]
Triangle: vertices=Triangle$Vertex2D@70dea4e [0.0, 0.0] [20.0, 0.0]
Triangle: vertices=Triangle$Vertex2D@5c647e05 [20.0, 0.0] [20.0, 0.0]
Triangle: vertices=Triangle$Vertex2D@33909752 [20.0, 0.0] [20.0, 0.0]
Triangle: vertices=Triangle$Vertex2D@33909752 [50.0, 20.0] [50.0, 20.0]
Triangle: vertices=Triangle$Vertex2D@55f96302 [20.0, 0.0] [40.0, 0.0]
Triangle: vertices=Triangle$Vertex2D@3d4eac69 [40.0, 0.0] [20.0, 0.0]
Triangle: vertices=Triangle$Vertex2D@42a57993 [-10.0, -20.0] [0.0, -40.0]
Triangle: vertices=Triangle$Vertex2D@42a57993 [-10.0, -20.0] [0.0, -40.0]
Triangle: vertices=Triangle$Vertex2D@42a57993 [-10.0, -20.0] [0.0, -40.0]
Triangle: vertices=Triangle$Vertex2D@75b84c92 [-10.0, -20.0] [-10.0, -60.0]
Triangle: vertices=Triangle$Vertex2D@6bc7c054 [-10.0, -60.0] [0.0, -40.0]
Triangle: vertices=Triangle$Vertex2D@232204a1 [-10.0, -20.0] [-10.0, -60.0]
Triangle: vertices=Triangle$Vertex2D@232204a1 [-20.0, -40.0] [-20.0, -80.0]
Triangle: vertices=Triangle$Vertex2D@4aa298b7 [-10.0, -20.0] [-20.0, -80.0]
Triangle: vertices=Triangle$Vertex2D@7d4991ad [-20.0, -80.0] [-10.0, -60.0]
Triangle: vertices=Triangle$Vertex2D@28d93b30 [-10.0, -60.0] [0.0, -40.0]
Triangle: vertices=Triangle$Vertex2D@28d93b30 [-10.0, -100.0] [0.0, -80.0]
Triangle: vertices=Triangle$Vertex2D@1b6d3586 [-10.0, -60.0] [-10.0, -100.0]
Triangle: vertices=Triangle$Vertex2D@4554617c [-10.0, -100.0] [0.0, -40.0]
Triangle: vertices=Triangle$Vertex2D@74a14482 [0.0, -40.0] [10.0, -20.0]
Triangle: vertices=Triangle$Vertex2D@74a14482 [20.0, -40.0] [30.0, -20.0]
Triangle: vertices=Triangle$Vertex2D@74a14482 [40.0, -40.0] [50.0, -20.0]
Triangle: vertices=Triangle$Vertex2D@1540e19d [20.0, -40.0] [50.0, -60.0]
Triangle: vertices=Triangle$Vertex2D@677327b6 [50.0, -60.0] [30.0, -20.0]
Triangle: vertices=Triangle$Vertex2D@14ae5a5 [0.0, -40.0] [10.0, -60.0]
Triangle: vertices=Triangle$Vertex2D@14ae5a5 [20.0, -80.0] [30.0, -100.0]
Triangle: vertices=Triangle$Vertex2D@7f31245a [0.0, -40.0] [10.0, -100.0]
Triangle: vertices=Triangle$Vertex2D@6d6f6e28 [10.0, -100.0] [10.0, -60.0]
Triangle: vertices=Triangle$Vertex2D@135fbaa4 [10.0, -60.0] [10.0, -20.0]
Triangle: vertices=Triangle$Vertex2D@135fbaa4 [40.0, -80.0] [40.0, -40.0]
Triangle: vertices=Triangle$Vertex2D@45ee12a7 [10.0, -60.0] [20.0, -80.0]
Triangle: vertices=Triangle$Vertex2D@330bedb4 [20.0, -80.0] [10.0, -20.0]
Triangle: vertices=Triangle$Vertex2D@15db9742 [0.0, 0.0] [20.0, 0.0]
Triangle: vertices=Triangle$Vertex2D@42a57993 [-10.0, -20.0] [0.0, -40.0]
Triangle: vertices=Triangle$Vertex2D@74a14482 [0.0, -40.0] [10.0, -20.0]
true