形状使用笛卡尔坐标向JFrame显示形状,得到NullPointerException错误



我读过讨论NullPointerExeception的类似链接,但我现在只是迷路了,我是Java的新手,我非常尊重任何提供的帮助。

我相信这个错误(来自本网站上的其他链接)来自于启动要在JFrame上绘制的turtle对象的初始位置。但是我做不到,我被卡住了。

该程序使用main类、抽象Shape类和扩展Shape类的Square类。它还使用了Turtle类(乌龟用drawLineBetweenPoints方法在屏幕上画线

这是main类:

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("WORKING CORRECTLY");
    }
    //ABSTRACT METHOD
    public abstract void draw();
}

这是Square类:

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();        
    }   
}

最后,这里是Turtle类:(我几乎复制了它的构造函数,让它与Shape类一起工作。)

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();         
    }
}

错误为

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)

您需要更改Shape构造函数,因为对于Turtle,canvas为null,并且您是可变阴影。

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(); 
}

最新更新