我想簡單地在Java中擴展一個抽象類並調用存儲在其中的幾個方法。當我這樣做時,我一直在收到NullPointerException。我在這裏錯過了關於抽象的東西嗎?從抽象類調用時發生Java NullPointerException
這是父類:
public abstract class Shape {
public Color color;
public Point center;
public double rotation;
public Shape() {
Color color = new Color();
Point center = new Point();
rotation = 0.0;
System.out.println("shape created");
}
public void setLocation(Point p) { center.locationX = p.locationX; center.locationY = p.locationY; }
public void setLocation(double x, double y) { center.locationX = x; center.locationY = y; }
public abstract double calcArea();
public abstract boolean draw();
}
而子類:
public class Ellipse extends Shape {
public Ellipse() {
}
public double calcArea() {
return 0.0;
}
public boolean draw() {
return true;
}
}
您可能希望看到點:
public class Point {
public double locationX;
public double locationY;
public Point() {
locationX = 0.0;
locationY = 0.0;
}
}
最後的主要功能:
public class MakeShapes {
public static void main(String []args) {
Ellipse myShapes = new Ellipse();
myShapes.setLocation(100.0, 100.0);
}
}
只要我使用setLocation(),我就得到了NPE。有什麼想法嗎?我的腦子因試圖弄清楚而感到痛苦。謝謝!!!
請提供異常的堆棧跟蹤,並在代碼中指明它出現的位置。 –