Java 使用OOP实现我的点和线类-错误:null指针异常
我正在尝试编写自己的Point and Line类(尽管类似的代码可以在网上找到)。 我这样做是为了练习如何从头开始构建基本代码。 我得到了一个空指针异常错误,我想有人帮我指出我做错了什么,以及如何解决这个问题。谢谢大家! 这是我的重点课程:Java 使用OOP实现我的点和线类-错误:null指针异常,java,oop,Java,Oop,我正在尝试编写自己的Point and Line类(尽管类似的代码可以在网上找到)。 我这样做是为了练习如何从头开始构建基本代码。 我得到了一个空指针异常错误,我想有人帮我指出我做错了什么,以及如何解决这个问题。谢谢大家! 这是我的重点课程: public class Point { private double x; private double y; public Point() { this.x = 0.0; this.y = 0.0; } public
public class Point {
private double x;
private double y;
public Point() {
this.x = 0.0;
this.y = 0.0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
public static void main(String[] args) {
Point p = new Point(1.5, 3);
System.out.println(p.toString());
}
}
这是我的线路课:
public class Line {
private Point p1;
private Point p2;
public Line() {
p1 = new Point(0,0);
p2 = new Point(0,0);
}
public Line(double x1, double y1, double x2, double y2) {
p1 = new Point(x1, y1);
p2 = new Point(x2, y2);
}
public Line(Point p1, Point p2) {
p1 = new Point(p1.getX(), p1.getY());
p2 = new Point(p2.getX(), p2.getY());
}
public Point getP1() {
return this.p1;
}
public Point getP2() {
return this.p2;
}
public void setP1(double x, double y) {
this.p1 = new Point (x, y);
}
public void setP2(double x, double y) {
this.p2 = new Point(x, y);
}
public double getSlope() {
return ((p2.getY() - p1.getY()) / (p2.getX() - p1.getX()));
}
public static void main(String[] args) {
Point p1 = new Point(0, 3);
Point p2 = new Point(5, 5);
Line l = new Line(p1, p2);
System.out.println(l.getSlope()); //Threw an error at this point!!
}
}
我的line类出现空指针异常错误,即使我的point is class是ok。
有人能解释一下如何解决这个问题吗?您需要调用this.p1/p2,因为这里您只更改构造函数的参数
public Line(Point p1, Point p2) {
this.p1 = new Point(p1.getX(), p1.getY());
this.p2 = new Point(p2.getX(), p2.getY());
}
无需更改Point类源代码
public class Point {
private double x;
private double y;
public Point() {
this.x = 0.0;
this.y = 0.0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
public static void main(String[] args) {
Point p = new Point(1.5, 3);
System.out.println(p.toString());
}
}
请按以下方式更改Line类中的代码
public class Line {
private Point p1;
private Point p2;
public Line() {
p1 = new Point(0,0);
p2 = new Point(0,0);
}
public Line(double x1, double y1, double x2, double y2) {
p1 = new Point(x1, y1);
p2 = new Point(x2, y2);
}
public Line(Point p1, Point p2) {
this. p1 = new Point(p1.getX(), p1.getY());//add this.p1 instead of p1
this.p2 = new Point(p2.getX(), p2.getY());//add this.p2 intead of p2
}
public Point getP1() {
return this.p1;
}
public Point getP2() {
return this.p2;
}
public void setP1(double x, double y) {
this.p1 = new Point (x, y);
}
public void setP2(double x, double y) {
this.p2 = new Point(x, y);
}
public double getSlope() {
return ((p2.getY() - p1.getY()) / (p2.getX() - p1.getX()));
}
public static void main(String[] args) {
Point p1 = new Point(0, 3);
Point p2 = new Point(5, 5);
Line l = new Line(p1, p2);
System.out.println(l.getSlope()); //Threw an error at this point!!
}
}
给定输入的输出:0.4
说明:
您正在尝试分配p1和p2引用变量(这是双参数重载行构造函数中的方法参数)类p1和p2的实例变量。但在这种情况下,方法还包含与类中的实例变量同名的方法参数,因此在这种情况下,方法参数将不会分配给类的实例变量。在新点(p1.getX()、p1.getY())和新点之后生成的对象(p2.getX(),p2.getY())将再次分配给方法参数p1和p2
解决方案1:(我在上面的代码中这样做了)
如果在实例变量和方法参数中使用相同的名称,则在处理实例变量时使用此关键字
解决方案2:
使用不同的变量名来定义方法参数和实例变量。谢谢。但同样的问题仍然存在。