好,所以我的程序應該根據用戶輸入的座標(X,Y,寬度,長度)自動繪製一個矩形。當我運行我的程序時,我在線程主錯誤中得到一個異常。線程「main」java.lang.NullPointerException錯誤中的異常。使用Gpdraw
這裏是確切的錯誤:
Exception in thread "main" java.lang.NullPointerException
at rectangle.draw(rectangle.java:31)
at rectangle.main(rectangle.java:52)
請告訴我,我在做什麼錯了!
謝謝!
代碼:`import gpdraw。*; import java.util.Scanner;
公共類矩形{
private static double myX;
private static double myY;
private static double myWidth;
private static double myHeight;
private DrawingTool myPencil;
private SketchPad myPaper;
public double getPerimeter(){
double perimeter;
perimeter = myWidth * 2 + myHeight * 2;
return perimeter;
}
public double Area(){
double area;
area = myHeight * myWidth;
System.out.println("Area: " + area);
return area;
}
public void draw(){
myPencil.up();
myPencil.move(myX , myY);
myPencil.down();
myPencil.move(myX + myWidth, myY);
myPencil.move(myX + myWidth, myY + myHeight);
myPencil.move(myX , myY);
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter X Value: ");
myX = input.nextInt();
System.out.println("Enter Y Value: ");
myY = input.nextInt();
System.out.println("Enter Width: ");
myWidth = input.nextInt();
System.out.println("Enter Height: ");
myHeight = input.nextInt();
rectangle picture = new rectangle();
picture.draw();
}
} `
線51:picture.draw(); 第31行:myPencil.up();
這是第31行?哪一行是52行? –
第31行:mypencil.up();第52行:picture.draw(); – iSully
你在哪裏分配什麼給mypencil? –