-5
    import acm.graphics.*;
    import acm.program.*;
    import acm.util.*;

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.EventListener;


public class Breakout1 extends GraphicsProgram {


public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;


private static final int WIDTH = APPLICATION_WIDTH;
private static final int HEIGHT = APPLICATION_HEIGHT;


private static final int PADDLE_WIDTH = 60;
private static final int PADDLE_HEIGHT = 10;


private static final int PADDLE_Y_OFFSET = 30;

/**Number of bricks per row */
private static final int NBRICKS_PER_ROW = 10;

/**
 * Number of rows of bricks
 */
private static final int NBRICK_ROWS = 10;

/**
 * Separation between bricks
 */
private static final int BRICK_SEP = 4;

/**
 * Width of a brick
 */
private static final int BRICK_WIDTH =
        (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;

/**
 * Height of a brick
 */
private static final int BRICK_HEIGHT = 8;

/**
 * Radius of the ball in pixels
 */
private static final int BALL_RADIUS = 10;

/**
 * Offset of the top brick row from the top
 */
private static final int BRICK_Y_OFFSET = 70;

/**
 * Number of turns
 */
private static final int NTURNS = 3;



/* Method: init() */

/**
 * Sets up the Breakout program.
 */
public void init() {
    add (new GImage("C:\\Users\\Home\\Desktop\\beach.jpg"));
    drawCanvas();
    drawBrickWall();
    drawPaddle();
    drawBall();
    addMouseListeners();
}


/* Method: run() */

/**
 * Runs the Breakout program.
 */
public void run() {


    }


private GRect brick;
private GOval ball;
private GRect paddle;

private void drawBrickWall() {
    for (int i = 0; i < NBRICK_ROWS; i++)
        for (int j = 0; j < NBRICKS_PER_ROW; j++)
            drawBrick(i, j);
}

private void drawBrick(int row, int col) {
    double x, y; // brick location
    GRect brick = new GRect(BRICK_WIDTH, BRICK_HEIGHT);
    x = computeXOffset() + col * (BRICK_WIDTH + BRICK_SEP);
    y = BRICK_Y_OFFSET + row * (BRICK_HEIGHT + BRICK_SEP);
    brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    brick.setFilled(true);
    add(brick, x, y);


    if (row < 2) {
        brick.setColor(Color.RED);
    } else if (row == 2 || row == 3) {
        brick.setColor(Color.ORANGE);
    } else if (row == 4 || row == 5) {
        brick.setColor(Color.YELLOW);
    } else if (row == 6 || row == 7) {
        brick.setColor(Color.GREEN);
    } else if (row == 8 || row == 9) {
        brick.setColor(Color.CYAN);
    }
}


private double computeXOffset() {
    return 0.5 * (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP - BRICK_WIDTH * NBRICKS_PER_ROW);
}

private void drawPaddle() {
    GRect paddle = new GRect(PADDLE_WIDTH, PADDLE_HEIGHT);
    paddle.setFilled(true);
    add(paddle, 0.5 * (WIDTH - PADDLE_WIDTH), HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT);
    addMouseListeners();
}

private boolean isMouseXInsideWindow(int x) {
    return ((x > PADDLE_WIDTH / 2) && (x < (WIDTH - PADDLE_WIDTH / 2)));
}


public void mouseMoved(MouseEvent e) {

    if (isMouseXInsideWindow(e.getX())) {
        paddle.setLocation(paddle.getX() - PADDLE_WIDTH / 2, paddle.getY());

    }

}

    private void drawBall(){
    GOval ball = new GOval(2*BALL_RADIUS, 2*BALL_RADIUS);
    ball.setFilled(true);
    add(ball, 0.5*WIDTH - BALL_RADIUS , 0.5*HEIGHT - BALL_RADIUS );
}

    private void drawCanvas(){
        GLabel myLabel= new GLabel ("Welcome to my Breakout Game!");
        myLabel.setFont("Serif-bold-24");
        myLabel.setColor(Color.CYAN);
        add(myLabel,getXCenter(myLabel), getYCenter(myLabel));


    }
    private double getXCenter (GObject g){

        return (getWidth()-g.getWidth())/2;
    }
    private double getYCenter (GObject g){

        return (getHeight()-g.getHeight())/2;
    }
}

it keeps giving me error NULLPOINTEXCEPTION as I run the program, Can someone try to check what is wrong with the mouseMoved(MouseEvent e), the paddle won't move and keeps giving me error. but the rest of the code are working.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • Post here an exact error (stacktrace). – ctomek Jun 04 '16 at 10:08
  • Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Breakout1.mouseMoved(Breakout1.java:169) at java.awt.AWTEventMulticaster.mouseMoved(AWTEventMulticaster.java:330) at java.awt.AWTEventMulticaster.mouseMoved(AWTEventMulticaster.java:329) at java.awt.Component.processMouseMotionEvent(Component.java:6580) – Camille Tan Jun 04 '16 at 10:16

1 Answers1

0

The Paddle you refer to as a data member doesn't seem to have been instantiated. You do create a Paddle, but it hasn't been assigned to the member variable, which is what you use in the rest of your code.

So, in your code, within drawPaddle(), you create a Paddle like this:

GRect paddle = new GRect(...).

However, this is never assigned to the class local variable paddle; therefore once drawPaddle() is called, your reference to the allocated variable is essentially lost. When you come to calling isMouseXInsideWindow(int), you try to use paddle.x; but this refers to the member variable paddle belonging to the class itself, which has been uninitialized.

So, when you originally allocate a paddle, it needs to be assigned to the classes' member variable instead of becoming a local variable. It's a real easy fix; within drawPaddle(), simply use paddle = new GRect(...) instead of GRect paddle = new GRect(...); this ensures we allocate the reference to the member variable paddle, rather than creating a local one whose reference we lose track of after the method has been called.

To avoid these problems in future, you should try designing your method calls so they expect a GRect to be passed into them. This allows you to be a lot more specific about how your methods work, and will force you to be careful with how you manage your references. Additionally, you'll find it'll help you a whole bunch if in future you wanted to add a second paddle, or a third or a fourth...

As a side note, whenever you're struggling with NullPointerExceptions, it helps to narrow down the exact source of the error. You already knew the problem had to lie somewhere in the mouseMoved(...) method, so you could try printing various variables you use to the console to see if they print something valid, print null, or don't even get printed at all (which indicates that the application terminated somewhere earlier than where you placed your print statement).

Mapsy
  • 4,192
  • 1
  • 37
  • 43