I have to write a program that has a tic tac toe board. When you click it, it should go from one image to another. My code compiles fine, but it doesnt run. I get some errors:
Exception in thread "main" java.lang.NullPointerException
at TicTacToe3.<init>(TicTacToe3.java:27)
at TicTacToe3.main(TicTacToe3.java:41)
My code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToe3 extends JFrame{
JButton x;
ImageIcon imageO;
JButton o;
ImageIcon imageX;
JButton[][] button = new JButton[3][3];
int i;
int j;
TicTacToe3(){
super("TicTacToe2");
setSize(600, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(3,3));
JButton[][] button = new JButton[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
button[i][j].addMouseListener(new ButtonListener());
add(button[i][j]);
}
}
ImageIcon imageX = new ImageIcon("kate.jpg");
button[i][j].setIcon(imageX);
setVisible(true);
}
public static void main(String args[]) {
TicTacToe3 e = new TicTacToe3();
}
class ButtonListener extends MouseAdapter {
public void mouseClicked(MouseEvent e){
ImageIcon imageO = new ImageIcon("jon.jpg");
button[i][j].setIcon(imageO);
}
}
}