井字游戏返回java.lang.NullPointerException

时间:2020-04-07 22:21:49

标签: java nullpointerexception tic-tac-toe

它在checkAdjacent方法内的if语句上引发异常,但可追溯到checkWinner方法。 checkAdjacent中的所有对象均未设置为null,因此必须设置其他对象。也许是JButtons对象之一?像空对象一样简单吗?我应该只是尝试处理异常吗?

package tictactoeb;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TicTacToe implements ActionListener
{
    String check[] =  new String[9];
    JButton buttons[] = new JButton[9];
    int num = 0;
    ImageIcon X = new ImageIcon("X.png");
    ImageIcon O = new ImageIcon("O.png");
    JPanel layout = new JPanel();
    JFrame window = new JFrame("TicTacToe");
    String letter = " ";
    ImageIcon img;
    String winner;
    public static void main(String[] args) 
    {
        new TicTacToe();
    }

    public TicTacToe() 
    {
        for(String s : check)
            s = " ";
        JFrame window = new JFrame("TicTacToe");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setBounds(400,400,400,400);
        window.setLayout(new GridLayout(3,3));
        for(int i = 0; i <= 8; i++)
        {
            buttons[i] = new JButton(" ");
            this.buttons[i] = buttons[i];
            buttons[i].setIcon(null);
            buttons[i].addActionListener(this);

            window.add(buttons[i]);  
        }
        window.setVisible(true);
    }


        @Override
    public void actionPerformed(ActionEvent e) 
    {
        JButton buttonClicked = (JButton)e.getSource(); 
        if(num%2 == 0)
        {   
            letter = "X";
            img = X;
        }
        else
        {
            letter = "O";
            img = O;
        }   
        for(int i = 0;i<9; ++i)
            if(buttonClicked.equals(buttons[i]))
            {
                buttons[i].setIcon(img);
                check[i] = letter;

            }
        if(checkWinner(letter))
        {
            System.out.println("Winner");
            clearBoard();
        }
        num++;

    }

    public boolean checkWinner(String ltr) 
    {
        boolean ref = false;
        if(checkAdjacent(0,1,ltr) && checkAdjacent(1,2,ltr))
            ref = true;
        else if(checkAdjacent(3,4,ltr) && checkAdjacent(4,5,ltr))
            ref = true;
        else if(checkAdjacent(6,7,ltr) && checkAdjacent(7,8,ltr))
            ref = true;

        else if(checkAdjacent(0,3,ltr) && checkAdjacent(3,6,ltr))
            ref = true;
        else if(checkAdjacent(1,4,ltr) && checkAdjacent(4,7,ltr))
            ref = true;
        else if(checkAdjacent(2,5,ltr) && checkAdjacent(5,8,ltr))
            ref = true;

        else if(checkAdjacent(0,4,ltr) && checkAdjacent(4,8,ltr))
            ref = true;
        else if(checkAdjacent(2,4,ltr) && checkAdjacent(4,6,ltr))
            ref = true;
        return ref;
    }

    public boolean checkAdjacent(int pos1,int pos2, String ltr) 
    {
        boolean ref = false;
        if(check[pos1].equals(ltr) && check[pos2].equals(ltr))
            ref = true;
        else
            ref = false;
        return ref;
    }

    public void clearBoard() 
    {
        for(JButton i : buttons)
        {
            i.setIcon(null);
        }
        for(String c : check)
            c = " ";
    }


}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at tictactoeb.TicTacToe.checkAdjacent(TicTacToe.java:109)
    at tictactoeb.TicTacToe.checkWinner(TicTacToe.java:85)
    at tictactoeb.TicTacToe.actionPerformed(TicTacToe.java:73)
    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:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6401)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    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(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    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(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at tictactoeb.TicTacToe.checkAdjacent(TicTacToe.java:109)
    at tictactoeb.TicTacToe.checkWinner(TicTacToe.java:85)
    at tictactoeb.TicTacToe.actionPerformed(TicTacToe.java:73)
    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:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6401)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    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(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    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(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    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)

0 个答案:

没有答案