1

I'm making a chessboard for a project and I have to use JButtons. I'm trying to just set the board up with a blank image I have for each tile, this is the code I have:

Driver

public class Driver
{
  public static void main (String[] args)
  {
    new ChessBoard();
  }
}

ChessSquare

import javax.swing.*;
import java.awt.*;

public class ChessSquare 
{
  public ImageIcon pieceImage;
  /** The square's location */
  private int xCoord;
  private int yCoord;

  /** Constructor for the squares */
  public ChessSquare(ImageIcon thePieceImage, int theXCoord, int theYCoord)
  {
    pieceImage = thePieceImage;
    xCoord = theXCoord;
    yCoord = theYCoord;
  }

  public int getXCoord()
  {
    return xCoord;
  }

  public int getYCoord()
  {
    return yCoord;
  }
}

ChessBoard

public class ChessBoard 
{
  public ChessBoard()
  {
  JFrame board = new JFrame();
  board.setTitle("Chess Board!");
  board.setSize(500,500);
  board.setLayout(new GridLayout(8,8));

  JPanel grid[][] = new JPanel[8][8];

  ImageIcon empty = new ImageIcon("/pieces/EmptySquare.jpg");

  for(int x = 0; x<8; x++)
  {
    for(int y = 0; y<8; y++)
    {
      ChessSquare s = new ChessSquare(empty, x, y);
      JButton square = new JButton(s.pieceImage);
      grid[x][y].add(square);
      board.setContentPane(grid[x][y]);
    }
  }
  board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  board.setVisible(true);
  }
}

My code compiles fine but when I run it I get this error:

Exception in thread "main" java.lang.NullPointerException at ChessBoard.(ChessBoard.java:23) at Driver.main(Driver.java:8)

I don't know what to do to fix this error. Thanks for any help :)

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user3326045
  • 33
  • 1
  • 6
  • The error is pointing to ChessBoard.java line 23. It will help find the issue quicker if you point out that line as well. – Sully Mar 25 '14 at 06:15
  • *"Making a Chess Board out of JButtons"* See [Making a robust, resizable Chess GUI](http://stackoverflow.com/q/21142686/418556) for a head start.. – Andrew Thompson Mar 26 '14 at 02:03

2 Answers2

3

In grid[x][y].add(square); you are actually calling JPanel.add(), because each element in the array is a JPanel.

So the error is because grid[x][y] is still null you will get a NullPointerException if you call a method on it, like add() in this case.

You want to assign the value since you are using an array

grid[x][y] = square;
Sully
  • 14,672
  • 5
  • 54
  • 79
  • Thanks a lot :) I changed the JPanels to JButtons in the grid like you said, and edited the path for the image as well, but now when it runs, it only loads one square to the screen instead of 64. Any ideas why? Thanks again – user3326045 Mar 25 '14 at 06:24
3

One of the likely causes is ImageIcon empty = new ImageIcon("/pieces/EmptySquare.jpg");...

The path of the image suggest that you are using embedded resources, but ImageIcon(String) treats the value as if it were a file, you can't do this with embedded resources, they aren't files.

Instead, you need to use something more like ImageIcon empty = new ImageIcon(getClass().getResource("/pieces/EmptySquare.jpg"));.

Personally, I'd recommend that you should be using ImageIO.read(getClass().getResource("/pieces/EmptySquare.jpg")) as this will throw an exception if the resource can not be loaded from some reason, rather then failing silently.

grid[x][y].add(square); also won't work, as you've not assigned anything to grid[x][y]

grid[x][y] = new JPanel();
grid[x][y].add(square);

Might work better, but I don't know why you're doing this, when doing something like...

JButton grid[][] = new JButton[8][8];

//...

grid[x][y] = square;

Would seem to be more logical for what you are trying to achieve...

Updated...

Instead of board.setContentPane(grid[x][y]); you should be using board.add(grid[x][y]);, other wise you will replace the content pane with the button...since there can only be a single content pane, you'll only get one button...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks a lot :) I changed the JPanels to JButtons in the grid like you said, and edited the path for the image as well, but now when it runs, it only loads one square to the screen instead of 64. Any ideas why? Thanks again. – user3326045 Mar 25 '14 at 06:22
  • Thanks, it's fine now :) how do I approve(?) your answer as the right one? – user3326045 Mar 25 '14 at 06:35