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);
            
         }
      }
   }

You have to actually create the JButtons in your array. You have only declared the array of references with this statement

JButton[][] button = new JButton[3][3];

You must still initialize those objects by adding

button[i][j] = new JButton();

in your loop before you try to do anything with them.

Edited 7 Years Ago by Ezzaral: n/a

Thanks! That makes perfect sense. That solves the whole window popping up thing, but I have still am having two probelms. My initial image icon only shows in the first button. Also, when I click a buton I get a long list of errors. Something like:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Any ideas?
Thanks again

Okay. I got all the images in the right spot. Now I just need to get them to switch to the other image.

New 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] = new JButton();
    ImageIcon imageX = new ImageIcon("kate.jpg"); 
    button[i][j].setIcon(imageX);
    button[i][j].addMouseListener(new ButtonListener());
       
    add(button[i][j]);
}
}

    

    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);
            
         }
      }
   }

Your listener is setting the icon on whichever button i and j corresponds to

button[i][j].setIcon(imageO);

What you really want to do is getSource() from the mouse event and cast that to a JButton and set it's image.

JButton clickedButton = (JButton)e.getSource();

When I take out:

button[i][j].setIcon(imageO);

and add:

JButton clickedButton = (JButton)e.getSource();

It no longer has error, but it doesnt swith. How do I pass it an image?

Call methods on "clickedButton". I wrote that to show you how to obtain a reference to the button that was clicked.

This article has been dead for over six months. Start a new discussion instead.