2016-05-04 8 views
-1

NullPointerExeceptionについて議論している類似のリンクを読みましたが、今は紛失しています。NullPointerExceptionエラーを受け取って、デカルト座標を使用してJFrameにシェイプを表示するシェイプ

私はこのエラー(このウェブサイト上の他のリンクから)はJFrameに描かれる "カメ"オブジェクトの最初の場所を開始したものと信じています。私はこれを行うことはできませんし、私は立ち往生しています。

プログラムは、メインクラス、抽象的なShapeクラス、およびShapeクラスを拡張するSquareクラスを使用しています。それはまた、(カメはdrawLineBetweenPoints方法で画面に線を描画タートルクラスを使用している。ここで

メインクラスです。

ここ
import javax.swing.*; 

class Lab4b 
{ 
    public static void main(String [] args) 
    { 
     JFrame frame = new JFrame(); 
     Canvas canvas = new Canvas(); 
     frame.setTitle("Hello Frame"); 
     frame.setSize(800, 600); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
     frame.add(canvas); 

     CartesianCoordinate position = new CartesianCoordinate(400, 300); 

     Turtle liam = new Turtle(canvas, position);  
     Shape square = new Square(canvas, position);   

     square.draw(); 
     System.out.println("X is: " + square.getSize());      
    } 
} 

はShapeクラスである:

abstract class Shape 
{ 
    protected double size = 100;  
    protected double angle = 0; 
    protected Canvas canvas; 
    protected CartesianCoordinate position = new CartesianCoordinate(400, 300); 
    protected Turtle turtle = new Turtle(canvas, position); 
    private CartesianCoordinate myLocation, oldLocation; 

    Shape(Canvas canvas, CartesianCoordinate position) 
    { 
     this.canvas = canvas; 
     Turtle turtle = new Turtle(canvas, position);  
     this.myLocation = new CartesianCoordinate(0,0); 

     myLocation = position.copy(); 
    } 

    //GETTERS 
    public double getSize() 
    { 
     return size; 
    } 
    public double getAngle() 
    { 
     return angle; 
    } 
    //SETTERS 
    public void setSize(double size) 
    { 
     size = this.size; 
    } 

    public void setAngle(double angle) 
    { 
     angle = this.angle; 
    } 

    public void noise() 
    { 
     System.out.println("FUCKING WORK"); 
    } 

    //ABSTRACT METHOD 

    public abstract void draw(); 

} 
ここ

は広場クラスです:

class Square extends Shape 
{ 

    Square(Canvas canvas, CartesianCoordinate position) 
    { 
    super(canvas, position); 
    this.draw(); 
    }  

    public void draw() 
    {   
    turtle.putPenDown();  
    turtle.turn(95); 
    turtle.move(100); 
    turtle.putPenDown(); 
    turtle.turn(95); 
    turtle.move(100); 
    turtle.putPenDown(); 
    turtle.turn(95); 
    turtle.move(100); 
    turtle.putPenDown(); 
    turtle.turn(95); 
    turtle.move(100); 
    turtle.putPenDown();   
    } 
} 

は最後に、ここではタートルクラスは次のとおりです。(私はほとんどコピーしましたそれは形状クラスで動作するように取得するコンストラクタです。)

class Turtle 
{ 
    private Canvas canvas; // private field reference to a canvas private   
    private CartesianCoordinate myLocation, oldLocation; 
    private CartesianCoordinate newPosition;  
    private boolean penDown = true; 
    private double Angle; 
    public Turtle kieranMullen; 

    public Turtle(Canvas canvas, CartesianCoordinate initLocation) 
    { 
     this.canvas = canvas; 
     this.myLocation = new CartesianCoordinate(0,0); 
     Angle = 0; 
     penDown = true; 
     myLocation = initLocation.copy();   
    } 

    public void putPenUp() 
    { 
     this.penDown = false; 
    } 

    public void putPenDown() 
    { 
     this.penDown = true; 
    } 

    public void goTo(CartesianCoordinate newPosition) 
    { 
     this.newPosition = myLocation; 
    } 

    public void turn(double amount) 
    { 
     Angle = Angle + amount; 
    } 


    public void move(int pixels) 
    { 
     double radians = Math.toRadians(Angle); 
     double dx = pixels * Math.sin(radians); 
     double dy = pixels * Math.cos(radians); 

     CartesianCoordinate oldLocation = myLocation.copy(); 

     myLocation.add(dx,dy); 

     if(penDown) 
     { 
      canvas.drawLineBetweenPoints(myLocation, oldLocation); 
     } 
    } 

    public void drawTurtle() 
    { 
     this.putPenDown(); 
     this.turn(90); 
     this.move(10); 
     this.putPenDown(); 
     this.turn(240); 
     this.move(20); 
     this.putPenDown(); 
     this.turn(240); 
     this.move(20); 
     this.putPenDown(); 
     this.turn(240); 
     this.move(10); 
     this.turn(270);  
    } 

    public void unDrawTurtle() 
    { 
     canvas.removeMostRecentLine(); 
     canvas.removeMostRecentLine(); 
     canvas.removeMostRecentLine(); 
     canvas.removeMostRecentLine(); 
    } 

    public void showSquare() 
    { 
     this.unDrawTurtle(); 
     Utils.pause(1000); 
     this.drawTurtle(); 
     Utils.pause(1000); 
     this.unDrawTurtle(); 
     this.move(100); 
     this.drawTurtle(); 
     Utils.pause(1000); 
     this.unDrawTurtle(); 
     this.turn(90); 
     this.drawTurtle(); 
     Utils.pause(1000); 
     this.unDrawTurtle(); 
     this.move(100); 
     this.drawTurtle(); 
     Utils.pause(1000); 
     this.unDrawTurtle(); 
     this.turn(90); 
     this.drawTurtle(); 
     Utils.pause(1000); 
     this.unDrawTurtle(); 
     this.move(100); 
     this.drawTurtle(); 
     Utils.pause(1000); 
     this.unDrawTurtle(); 
     this.turn(90); 
     this.drawTurtle(); 
     Utils.pause(1000); 
     this.unDrawTurtle(); 
     this.move(100); 
     this.drawTurtle(); 
     Utils.pause(1000); 
     this.unDrawTurtle(); 
     this.turn(90); 
     this.drawTurtle(); 
     Utils.pause(1000); 
     this.unDrawTurtle();   
    } 
} 

エラーがcanvasがカメのためにnullで、あなたがvariable shadowingであるため、シェイプのコンストラクタを変更する必要がある

Exception in thread "main" java.lang.NullPointerException 
    at Turtle.move(Turtle.java:52) 
    at Square.draw(Square.java:14) 
    at Square.<init>(Square.java:7) 
    at Lab4b.main(Lab4b.java:21) 
+0

誰でも救い出すことができれば幸いです!前もって感謝します! Square.draw(Square.java:14)スクエア でTurtle.move(Turtle.java:52)でスレッド "メイン" のjava.lang.NullPointerException で 例外 : エラー私は取得しています。 (Square.java:7) Lab4b.mainで(Lab4b.java:21) C:\コード\ LAB4>のJava Lab4a 速度:100 C:\コード\ LAB4> – jHornsby

+0

再初期化'タートルカメ=新しいタートル(キャンバス、位置);'スクエア 'クラス。 –

+0

あなたの質問を間違えて[編集]してください。 Turtle.javaの52行はどちらの行に言及してください。そして、あなたは他のNullPointerQuestionsについてどうして正確に理解できませんでしたか? –

答えて

0

です。

protected Canvas canvas; 
protected Turtle turtle; // <-- Remove declaration here 

Shape(Canvas canvas, CartesianCoordinate position) 
{ 
    this.canvas = canvas; 
    this.turtle = new Turtle(canvas, position); // <--- Do it here 
    // this.myLocation = new CartesianCoordinate(0,0); // <-- Not needed 
    this.myLocation = position.copy(); 
} 
関連する問題