Forum Stats

  • 3,872,882 Users
  • 2,266,487 Discussions


Null Pointer Exception

Hello all. I am a newbie. I have completed one college class on Java. I am trying to develop a simple tic-tac-toe game. I have 2 classes. One is TicTacToe and the other is TicTacToeBoard. Here is the code I have so far.

class TicTacToe {
TicTacToeBoard board = new TicTacToeBoard();     private int moves = 0;
    private int Gamemoves;
        public static void main(String[] args) {         TicTacToe game = new TicTacToe();
    public void startGame() {
    public int getMoves() {
        return moves;
    public void setMoves(int moves) {
        this.moves = moves;

public class TicTacToeBoard extends javax.swing.JFrame {
    TicTacToe game;
    int mmSet, ulSet, umSet, urSet, mlSet, mrSet, llSet, lmSet, lrSet;
    int NumMoves = 0;

public TicTacToeBoard() {
    } private void btnMMStateChanged(javax.swing.event.ChangeEvent evt) {                                           if (mmSet == 0) {
            mmSet = 1;             NumMoves = game.getMoves();             System.out.println(NumMoves);
            btnMM.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tic/tac/toe/images/X.png")));
            stripMouseListeners((java.awt.Component) btnMM);         } else {

The problem occurs 12 of TicTacToeBoard class. I have read about this problem and it seems like my problem is that I am not properly using the game variable. I am not sure where to go from here. Here is the stack trace.

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

at tic.tac.toe.TicTacToeBoard.btnMMStateChanged(

at tic.tac.toe.TicTacToeBoard.access$400(

at tic.tac.toe.TicTacToeBoard$5.stateChanged(

at javax.swing.AbstractButton.fireStateChanged(

at javax.swing.AbstractButton$Handler.stateChanged(

at javax.swing.DefaultButtonModel.fireStateChanged(

at javax.swing.DefaultButtonModel.setArmed(

at javax.swing.plaf.basic.BasicButtonListener.mousePressed(

at java.awt.Component.processMouseEvent(

at javax.swing.JComponent.processMouseEvent(

at java.awt.Component.processEvent(

at java.awt.Container.processEvent(

at java.awt.Component.dispatchEventImpl(

at java.awt.Container.dispatchEventImpl(

at java.awt.Component.dispatchEvent(

at java.awt.LightweightDispatcher.retargetMouseEvent(

at java.awt.LightweightDispatcher.processMouseEvent(

at java.awt.LightweightDispatcher.dispatchEvent(

at java.awt.Container.dispatchEventImpl(

at java.awt.Window.dispatchEventImpl(

at java.awt.Component.dispatchEvent(

at java.awt.EventQueue.dispatchEventImpl(

at java.awt.EventQueue.access$200(

at java.awt.EventQueue$

at java.awt.EventQueue$

at Method)



at java.awt.EventQueue$

at java.awt.EventQueue$

at Method)


at java.awt.EventQueue.dispatchEvent(

at java.awt.EventDispatchThread.pumpOneEventForFilters(

at java.awt.EventDispatchThread.pumpEventsForFilter(

at java.awt.EventDispatchThread.pumpEventsForHierarchy(

at java.awt.EventDispatchThread.pumpEvents(

at java.awt.EventDispatchThread.pumpEvents(


Can someone please explain the proper way for me to try to get / set the number of moves from the TicTacToeBoard class?



This discussion has been closed.