简体   繁体   中英

Shape displaying shapes to JFrame using cartesian coordinates, getting a NullPointerException error

I have read similar links discussing NullPointerExeception , however I am just lost now, I am new to Java and would greatly respect any help given.

I believe this error(from the other links on this website) come from initiating the initial location for the turtle object to be drawn on the JFrame . I cannot however do this and I'm stuck.

The program is using a main class, an abstract Shape class, and a Square class that extends the Shape class. It is also using a Turtle class(the turtle draws the line to the screen with a drawLineBetweenPoints method.

Here is the main class:

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

Here is the Shape class:

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

}

Here is the Square class:

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

Finally, here is the Turtle class: (I've almost copied it's constructor to get it to work with the Shape class.)

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

The error is

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)

You are need to change the Shape constructor because canvas is null for the Turtle and you are variable shadowing .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM