I get Exception in thread "main" java.lang.NullPointerException
at project2.SuperTicTacToePanel.<init>(SuperTicTacToePanel.java:41)
at project2.SuperTicTacToe.main(SuperTicTacToe.java:27)
at project2.SuperTicTacToePanel.<init>(SuperTicTacToePanel.java:41)
at project2.SuperTicTacToe.main(SuperTicTacToe.java:27)
public class SuperTicTacToe {
/**
*
*/
public static void main (String[] args){
JFrame frame = new JFrame ("SuperTicTacToe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add (new SuperTicTacToePanel());
frame.pack();
frame.setVisible(true);
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class SuperTicTacToePanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton[][] board;
private Cell[][] cells;
private JButton quitButton;
private ImageIcon xIcon;
private ImageIcon oIcon;
private ImageIcon emptyIcon;
private SuperTicTacToeGame game;
public SuperTicTacToePanel() {
game = new SuperTicTacToeGame();
ButtonListener listener = new ButtonListener();
quitButton.addActionListener(listener);
quitButton = new JButton("Quit");
JPanel buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(400, 400));
buttonPanel.setBackground(Color.MAGENTA);
JPanel K = new JPanel();
K.setPreferredSize(new Dimension(50, 400));
K.setBackground(Color.MAGENTA);
K.add(quitButton);
xIcon = new ImageIcon("x.jpg");
oIcon = new ImageIcon("o.jpg");
emptyIcon = new ImageIcon("empty.jpg");
buttonPanel.setLayout(new GridLayout(3, 3));
add(buttonPanel);
add(K);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
this.board[row][col] = new JButton("", emptyIcon);
board[row][col].addActionListener(listener);
buttonPanel.add(board[row][col]);
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
this.board[row][col] = new JButton("", xIcon);
board[row][col].addActionListener(listener);
buttonPanel.add(board[row][col]);
}
}
for (int row = 0; row < 3; row++){
for (int col = 0; col < 3; col++) {
this.board[row][col] = new JButton("", oIcon);
board[row][col].addActionListener(listener);
buttonPanel.add(board[row][col]);
}}
}
private void displayBoard() {
cells = game.getBoard();
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (cells[row][col] == Cell.X) {
board[row][col].setIcon(xIcon);
}
if (cells[row][col] == Cell.O) {
board[row][col].setIcon(oIcon);
}
if (cells[row][col] == Cell.EMPTY) {
board[row][col].setIcon(emptyIcon);
}
}
}
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event){
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (board[row][col] == event.getSource()) {
// tell the game which button was selected.
game.select(row,col);
}
}
}
if( quitButton == null)
{
System.out.println("stop");
}
if (event.getSource() == quitButton)
{
System.exit(0);
}
displayBoard();
if (game.getGameStatus() == GameStatus.O_WON) {
JOptionPane.showMessageDialog(null, "O won and X + lost!\n The game will reset");
}
if (game.getGameStatus() == GameStatus.X_WON) {
JOptionPane.showMessageDialog(null, "X won and O + lost!\n The game will reset");
}
}
}
}