Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Breakout Game Error

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Breakout Game Error

    Hi All,

    I'm having an issue with the following error, I think its due to the placement of my images? Can someone confirm that:

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at brick.Ball.<init>(Ball.java:18)
    at brick.Board.gameInit(Board.java:50)
    at brick.Board.addNotify(Board.java:45)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at javax.swing.JRootPane.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at java.awt.Window.addNotify(Unknown Source)
    at java.awt.Frame.addNotify(Unknown Source)
    at java.awt.Window.show(Unknown Source)
    at java.awt.Component.show(Unknown Source)
    at java.awt.Component.setVisible(Unknown Source)
    at java.awt.Window.setVisible(Unknown Source)
    at brick.BrickGame.<init>(BrickGame.java:16)
    at brick.BrickGame.main(BrickGame.java:20)
    Exception in thread "Timer-0" java.lang.NullPointerException
    at brick.Board$ScheduleTask.run(Board.java:112)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Breakout Game Error

    Not without an SSCCE we can't, no.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Breakout Game Error

    Heres the code:

    Board.java


    package brick;

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Point;
    import java.awt.Toolkit;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;

    import java.util.Timer;
    import java.util.TimerTask;

    import javax.swing.JPanel;


    public class Board extends JPanel implements Commons {

    Image ii;
    Timer timer;
    String message = "Game Over";
    Ball ball;
    Paddle paddle;
    Brick bricks[];

    boolean ingame = true;
    int timerId;


    public Board() {

    addKeyListener(new TAdapter());
    setFocusable(true);

    bricks = new Brick[30];
    setDoubleBuffered(true);
    timer = new Timer();
    timer.scheduleAtFixedRate(new ScheduleTask(), 1000, 10);
    }

    public void addNotify() {
    super.addNotify();
    gameInit();
    }

    public void gameInit() {

    ball = new Ball();
    paddle = new Paddle();


    int k = 0;
    for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 6; j++) {
    bricks[k] = new Brick(j * 40 + 30, i * 10 + 50);
    k++;
    }
    }
    }


    public void paint(Graphics g) {
    super.paint(g);

    if (ingame) {
    g.drawImage(ball.getImage(), ball.getX(), ball.getY(),
    ball.getWidth(), ball.getHeight(), this);
    g.drawImage(paddle.getImage(), paddle.getX(), paddle.getY(),
    paddle.getWidth(), paddle.getHeight(), this);

    for (int i = 0; i < 30; i++) {
    if (!bricks[i].isDestroyed())
    g.drawImage(bricks[i].getImage(), bricks[i].getX(),
    bricks[i].getY(), bricks[i].getWidth(),
    bricks[i].getHeight(), this);
    }
    } else {

    Font font = new Font("Verdana", Font.BOLD, 18);
    FontMetrics metr = this.getFontMetrics(font);

    g.setColor(Color.BLACK);
    g.setFont(font);
    g.drawString(message,
    (Commons.WIDTH - metr.stringWidth(message)) / 2,
    Commons.WIDTH / 2);
    }


    Toolkit.getDefaultToolkit().sync();
    g.dispose();
    }

    private class TAdapter extends KeyAdapter {

    public void keyReleased(KeyEvent e) {
    paddle.keyReleased(e);
    }

    public void keyPressed(KeyEvent e) {
    paddle.keyPressed(e);
    }
    }


    class ScheduleTask extends TimerTask {

    public void run() {

    ball.move();
    paddle.move();
    checkCollision();
    repaint();

    }
    }

    public void stopGame() {
    ingame = false;
    timer.cancel();
    }


    public void checkCollision() {

    if (ball.getRect().getMaxY() > Commons.BOTTOM) {
    stopGame();
    }

    for (int i = 0, j = 0; i < 30; i++) {
    if (bricks[i].isDestroyed()) {
    j++;
    }
    if (j == 30) {
    message = "Victory";
    stopGame();
    }
    }

    if ((ball.getRect()).intersects(paddle.getRect())) {

    int paddleLPos = (int)paddle.getRect().getMinX();
    int ballLPos = (int)ball.getRect().getMinX();

    int first = paddleLPos + 8;
    int second = paddleLPos + 16;
    int third = paddleLPos + 24;
    int fourth = paddleLPos + 32;

    if (ballLPos < first) {
    ball.setXDir(-1);
    ball.setYDir(-1);
    }

    if (ballLPos >= first && ballLPos < second) {
    ball.setXDir(-1);
    ball.setYDir(-1 * ball.getYDir());
    }

    if (ballLPos >= second && ballLPos < third) {
    ball.setXDir(0);
    ball.setYDir(-1);
    }

    if (ballLPos >= third && ballLPos < fourth) {
    ball.setXDir(1);
    ball.setYDir(-1 * ball.getYDir());
    }

    if (ballLPos > fourth) {
    ball.setXDir(1);
    ball.setYDir(-1);
    }


    }


    for (int i = 0; i < 30; i++) {
    if ((ball.getRect()).intersects(bricks[i].getRect())) {

    int ballLeft = (int)ball.getRect().getMinX();
    int ballHeight = (int)ball.getRect().getHeight();
    int ballWidth = (int)ball.getRect().getWidth();
    int ballTop = (int)ball.getRect().getMinY();

    Point pointRight =
    new Point(ballLeft + ballWidth + 1, ballTop);
    Point pointLeft = new Point(ballLeft - 1, ballTop);
    Point pointTop = new Point(ballLeft, ballTop - 1);
    Point pointBottom =
    new Point(ballLeft, ballTop + ballHeight + 1);

    if (!bricks[i].isDestroyed()) {
    if (bricks[i].getRect().contains(pointRight)) {
    ball.setXDir(-1);
    }

    else if (bricks[i].getRect().contains(pointLeft)) {
    ball.setXDir(1);
    }

    if (bricks[i].getRect().contains(pointTop)) {
    ball.setYDir(1);
    }

    else if (bricks[i].getRect().contains(pointBottom)) {
    ball.setYDir(-1);
    }

    bricks[i].setDestroyed(true);
    }
    }
    }
    }
    }



    Paddle.java



    package brick;

    import java.awt.event.KeyEvent;

    import javax.swing.ImageIcon;


    public class Paddle extends Sprite implements Commons {

    String paddle = "../images/paddle.png";

    int dx;

    public Paddle() {

    ImageIcon ii = new ImageIcon(this.getClass().getResource(paddle));
    image = ii.getImage();

    width = image.getWidth(null);
    heigth = image.getHeight(null);

    resetState();

    }

    public void move() {
    x += dx;
    if (x <= 2)
    x = 2;
    if (x >= Commons.PADDLE_RIGHT)
    x = Commons.PADDLE_RIGHT;
    }

    public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
    dx = -2;

    }

    if (key == KeyEvent.VK_RIGHT) {
    dx = 2;
    }
    }

    public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
    dx = 0;
    }

    if (key == KeyEvent.VK_RIGHT) {
    dx = 0;
    }
    }

    public void resetState() {
    x = 200;
    y = 360;
    }
    }



    Ball.java



    package brick;

    import javax.swing.ImageIcon;


    public class Ball extends Sprite implements Commons {

    private int xdir;
    private int ydir;

    protected String ball = "../images/ball.png";

    public Ball() {

    xdir = 1;
    ydir = -1;

    ImageIcon ii = new ImageIcon(this.getClass().getResource(ball));
    image = ii.getImage();

    width = image.getWidth(null);
    heigth = image.getHeight(null);

    resetState();
    }


    public void move()
    {
    x += xdir;
    y += ydir;

    if (x == 0) {
    setXDir(1);
    }

    if (x == BALL_RIGHT) {
    setXDir(-1);
    }

    if (y == 0) {
    setYDir(1);
    }
    }

    public void resetState()
    {
    x = 230;
    y = 355;
    }

    public void setXDir(int x)
    {
    xdir = x;
    }

    public void setYDir(int y)
    {
    ydir = y;
    }

    public int getYDir()
    {
    return ydir;
    }
    }



    BrickGame.java



    package brick;

    import javax.swing.JFrame;

    public class BrickGame extends JFrame {

    public BrickGame()
    {
    add(new Board());
    setTitle("Breakout");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(Commons.WIDTH, Commons.HEIGTH);
    setLocationRelativeTo(null);
    setIgnoreRepaint(true);
    setResizable(false);
    setVisible(true);
    }

    public static void main(String[] args) {
    new BrickGame();
    }
    }


    Sprite.java

    package brick;

    import java.awt.Image;
    import java.awt.Rectangle;

    public class Sprite {

    protected int x;
    protected int y;
    protected int width;
    protected int heigth;
    protected Image image;


    public void setX(int x) {
    this.x = x;
    }

    public int getX() {
    return x;
    }

    public void setY(int y) {
    this.y = y;
    }

    public int getY() {
    return y;
    }

    public int getWidth() {
    return width;
    }

    public int getHeight() {
    return heigth;
    }

    Image getImage()
    {
    return image;
    }

    Rectangle getRect()
    {
    return new Rectangle(x, y,
    image.getWidth(null), image.getHeight(null));
    }
    }

    Commons.java


    package brick;

    public interface Commons {
    public static final int WIDTH = 300;
    public static final int HEIGTH = 400;
    public static final int BOTTOM = 390;
    public static final int PADDLE_RIGHT = 250;
    public static final int BALL_RIGHT = 280;
    }

    [B]Brick.java[B]

    package brick;

    import javax.swing.ImageIcon;


    public class Brick extends Sprite {

    String brickie = "../images/brickie.png";

    boolean destroyed;


    public Brick(int x, int y) {
    this.x = x;
    this.y = y;

    ImageIcon ii = new ImageIcon(this.getClass().getResource(brickie));
    image = ii.getImage();

    width = image.getWidth(null);
    heigth = image.getHeight(null);

    destroyed = false;
    }

    public boolean isDestroyed()
    {
    return destroyed;
    }

    public void setDestroyed(boolean destroyed)
    {
    this.destroyed = destroyed;
    }

    }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Breakout Game Error

    Right. But we need an SSCCE. There are simply too many posts here for us to wade through that much code. Get the problem boiled down to a few lines, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Breakout Game Error

    The lines in bold have been highlighted by the compiler:

    public Ball() {

    xdir = 1;
    ydir = -1;

    ImageIcon ii = new ImageIcon(this.getClass().getResource(ball));
    image = ii.getImage();

    width = image.getWidth(null);
    heigth = image.getHeight(null);

    resetState();
    }


    public void gameInit() {

    ball = new Ball();
    paddle = new Paddle();

    public void addNotify() {
    super.addNotify();
    gameInit();
    }

    class ScheduleTask extends TimerTask {

    public void run() {

    ball.move();
    paddle.move();
    checkCollision();
    repaint();

    }

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Breakout Game Error

    Now you're getting somewhere. What are each of the compiler errors?

    Hint: for the last three errors, it's a problem of scope. Where do you declare a variable named ball? Where do you define a method named gameInit?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Breakout Game
    By hnc in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 30th, 2011, 10:24 AM
  2. Game of Life GUI Error
    By Lavace in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 3rd, 2011, 09:15 AM
  3. Breakout Game- program help
    By strength.honor in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 5th, 2010, 02:44 PM
  4. Error in Program for Game of Craps
    By TheAsianMenace in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 04:31 AM
  5. Breakout Game
    By Ceasar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2009, 12:30 AM