获取空指针异常错误空值被传递

Posted

技术标签:

【中文标题】获取空指针异常错误空值被传递【英文标题】:Getting Null Pointer Exception error Null value getting passed 【发布时间】:2014-05-09 01:05:36 【问题描述】:

我想创建一个三角形对象并获取三角形的面积和周长。我在Triangle.areaDemo.main 收到NullPointerException。似乎没有值被传递给 area 方法和 perimeter 方法,但我看不出我的代码哪里出错了。这是我的代码。

//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());   
    

【问题讨论】:

【参考方案1】:

您在创建Triangle 对象时调用了构造函数Triangle(Point[] vertices)。所以你的这些字段保持未初始化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】:

您调用构造函数public Triangle(Point[] vertices) 并且您没有设置point1、point2 和point3 值——它们保持为空。因此,当您在方法区域中尝试计算 double side1 = point1.distance(point2); 时,它会抛出 NPE。

【讨论】:

【参考方案3】:

尝试将整个代码放在 try-catch 块中并捕获 NullPointerException 并编写

 e.printStackTrace(); 

例如:

try

    //your block of code

catch(NullPointerException e)

    e.printStackTrace();

它会显示错误代码的行号

【讨论】:

以上是关于获取空指针异常错误空值被传递的主要内容,如果未能解决你的问题,请参考以下文章

c++异步回调函数引用传递空指针异常

哪些编程语言会阻止空指针异常?

Kotlin 中的非空值产生空指针异常

Java8新特性Optional类在处理空值判断场景的应用 回避空指针异常 编写健壮的应用程序

列表视图中的空指针异常?

空指针异常