当前位置: 代码迷 >> Eclipse >> 请大家帮小弟我检查异常
  详细解决方案

请大家帮小弟我检查异常

热度:76   发布时间:2016-04-23 13:59:01.0
请大家帮我检查错误
package Test;

class Point{
double x;
double y;
public Point(double _x,double _y){
x = _x;
y = _y;
}
public double getX()
{
return x;
}
public void setX(double x)
{
this.x = x;
}
public double getY()
{
return y;
}
public void setY(double y)
{
this.y = y;
}

}
class Circle{
private Point o;
private double r;
public Circle(Point p,double r){
p = p;
r = r;

}
public Point getP()
{
return o;
}
public void setP(Point p)
{
this.o = p;
}
public double getR()
{
return r;
}
public void setR(double r)
{
this.r = r;
}
boolean disance(Point p){//包不包含一个点就传一个点对象进来
double x = p.getX()-o.getX();
double y = p.getY()-o.getY();
if(x * x + y * y > r * r){
return false;
}
else return true;
}

}

public class Test {
public static void main(String[] args)
{
Circle c = new Circle(new Point(10, 10), 5);
Point p1 = new Point(11, 11);
System.out.println(c.disance(p1));
}
}
这个程序主要实现测试一个点是不是包含在一个园内,程序经运行提示空指针,自己仔细分析就是找不出为什么,请高手指明,谢谢

------解决方案--------------------
Circle的构造方法有问题,导致o和r 没有设置成功,改成下面的就OK了
Java code
class Point{    double x;    double y;    public Point(double _x,double _y){        x = _x;        y = _y;    }    public double getX()    {        return x;    }    public void setX(double x)    {        this.x = x;    }    public double getY()    {        return y;    }    public void setY(double y)    {        this.y = y;    }}class Circle{    private Point o;    private double r;    public Circle(Point p,double r){        this.o = p; //这里进行修改        this.r = r; //这里进行修改    }    public Point getP()    {        return o;    }    public void setP(Point p)    {        this.o = p;    }    public double getR()    {        return r;    }    public void setR(double r)    {        this.r = r;    }    boolean disance(Point p){//包不包含一个点就传一个点对象进来        double x = p.getX()-o.getX();        double y = p.getY()-o.getY();        if(x * x + y * y > r * r){            return false;        }        else return true;    }}public class Test {    public static void main(String[] args)    {        Circle c = new Circle(new Point(10, 10), 5);        Point p1 = new Point(11, 11);        System.out.println(c.disance(p1));    }}
------解决方案--------------------
Java code
class Point {    double x;    double y;    public Point(double _x, double _y) {        x = _x;        y = _y;    }    public double getX() {        return x;    }    public void setX(double x) {        this.x = x;    }    public double getY() {        return y;    }    public void setY(double y) {        this.y = y;    }}class Circle {    private Point o;    private double r;    public Circle(Point p, double r) {        // error        // p = p;        this.o = p;        this.r = r;    }    public Point getP() {        return o;    }    public void setP(Point p) {        this.o = p;    }    public double getR() {        return r;    }    public void setR(double r) {        this.r = r;    }    boolean disance(Point p) {// 包不包含一个点就传一个点对象进来        double x = p.getX() - o.getX();        double y = p.getY() - o.getY();        if (x * x + y * y > r * r) {            return false;        } else            return true;    }}public class Main {    public static void main(String[] args) {        Circle c = new Circle(new Point(10, 10), 5);        Point p1 = new Point(11, 11);        System.out.println(c.disance(p1));    }}
  相关解决方案