0

I wish to create a simple TicTacToe GUI Game by using Swing; I created JFrame called TicTacToe and put JPanel in it called gamePanel with GridLayout(3,3). Then I created a button array called buttons[][] for my buttons, in which I'll put icon of X or O. Then I generated 9 buttons and added logic. JPanel code:

public class gamePanel extends JPanel {
XOButton buttons[][] = new XOButton[3][3];
boolean isX;
public gamePanel() {
    setLayout(new GridLayout(3, 3));
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++) {
            buttons[i][j] = new XOButton();
            buttons[i][j].setIcon(null);
            add(buttons[i][j]);
        }
    }
    isX = true;
}

private class XOButton extends JButton {
    ImageIcon X, O;

    public XOButton() {
        X = new ImageIcon(gamePanel.this.getClass().getResource("X.png"));
        O = new ImageIcon(gamePanel.this.getClass().getResource("O.png"));
        addActionListener(this::actionPerformed);
    }

    public int getValue(XOButton btn){
        if(btn.getIcon().toString().equals(null)){
            return 0;
        }
        if(btn.getIcon().toString().equals(X.toString())){
            return 1;
        }
        if(btn.getIcon().toString().equals(O.toString())){
            return 2;
        }
        return 0;
    }

    public void actionPerformed(ActionEvent e) {
        if(getIcon() != null) return;
            if (isX) {
                setIcon(X);
                isX = false;
                checkWinner();
            } else {
                setIcon(O);
                isX = true;
                checkWinner();
            }
    }

    public void checkWinner(){
            for(int i = 0; i < 3; i++){
            if(getValue(buttons[i][0]) != 0 && getValue(buttons[i][0]) == getValue(buttons[i][1])
                && getValue(buttons[i][1]) == (getValue(buttons[i][2]))) {
                if (getValue(buttons[i][0]) == 1) {
                    JOptionPane.showMessageDialog(null, X);
                    return;
                } else if (getValue(buttons[i][0]) == 2) {
                    JOptionPane.showMessageDialog(null, O);
                    return;
                }
            }
        }
    }
}

}

I was wondering if I can get an answer for the problem (please!): As you see, i created procedure called checkWinnerand yet it is only checking if button icons are equal in one row. But when I use this code, it works properly only for first row. The second and third are ignoring until I will fill the first two buttons of first row. When code works, it should show message with an image of X or O (depends on who is winner). Image where it works fine in the first row and Image where it is not working (until I will fill first row for a half) When I'll fill first row in second image it will display both two messageDialogs with X and O image.

What am I doing wrong? I'll also appreciate answers and comments about my code, it's terrible and I want to improve my skills. Thanks for paying attention.

Edit: I get this exception when pressing any of the button:

`Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gamePanel$XOButton.getValue(gamePanel.java:34)
at gamePanel$XOButton.checkWinner(gamePanel.java:61)
at gamePanel$XOButton.actionPerformed(gamePanel.java:51)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:270)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6589)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6354)
at java.desktop/java.awt.Container.processEvent(Container.java:2260)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4966)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2318)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4906)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2304)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue.access$600(EventQueue.java:97)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

`

nklymok
  • 35
  • 9
  • You should either run a debugger or have checkWinner dump out it's parameters when it starts. At a glance I don't see anything that should prevent it from working, but I didn't make sure your board is being filled in correctly. – Bill K Jun 21 '18 at 19:50
  • I forgot to tell that I get these errrors when pressing any buttons. – nklymok Jun 21 '18 at 20:12
  • there are 8 cases for win condition. don't itterate just check each case. HORIZ:[0][0],[1][0],[2][0] [0][1],[1][1],[2][1] [0][2],[1][2],[2][2] VERT: [0][0],[0][1],[0][2] [1][0],[1][1],[1][2] [2][0],[2][1],[2][2] DIAG: [0][0],[1][1],[2][2] [2][2],[1][1],[0][0]. check if all elements are equal – mavriksc Jun 21 '18 at 20:18

2 Answers2

2

In getValue:

btn.getIcon().toString().equals(null)

btn.getIcon() is null for an unclicked Button. The toString() therefore raises a NPE.

Try to change this to

if(btn.getIcon() != null)

the same for the other if's

if(btn.getIcon() != null && btn.getIcon().equals(X))

instead of

 if(btn.getIcon().toString().equals(X.toString()))
frank
  • 1,007
  • 1
  • 6
  • 13
  • Thanks alot for your answer! Looks like it should work. But I should use toString() because without it I cant compare button icons. btn.getIcon().equals(X) is not working. – nklymok Jun 21 '18 at 20:24
  • I added toString() and it works perfectly! Thanks, frank! – nklymok Jun 21 '18 at 20:30
0

In getValue(XOButton btn) this line

if(btn.getIcon().toString().equals(null))

throws a NullPointerException if the icon has not yet been set.

The icon is only set if you click on a button.

Replace your first condition in getValue(XOButton btn) with

    if(btn.getIcon() == null){
        return 0;
    }

If the icon has been set (the other two cases) won't be a NullPointerException.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
  • Exactly! Thanks, Thomas! I had read that original question before I asked this, but I handled exceptions badly. – nklymok Jun 21 '18 at 20:31