Java错误:带有arrayList按钮的TicTacToe游戏

时间:2017-02-19 01:21:41

标签: java arraylist

所以基本上我有一个tictactoe程序,我想通过使用arrayList和for循环为我的按钮和动作侦听器最小化代码的数量。没有编译错误,但是当我运行它时,它会说明这一点。

java.lang.NullPointerException
at Board.<init>(Board.java:40)
at Board.main(Board.java:166)

这是BlueJ向我展示的错误。无论如何,这是我的代码。我怀疑它与实例变量和arrayList有关。请帮忙,我一直试图修复它两天。错误将我引导到这一行。

for(int i=0; i<=buttonsList.size(); i++){

这是代码。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

//Created class the extends JFrame, and implements action listener
public class Board extends JFrame implements ActionListener 
{
//Instance variables 
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, reset;
private ArrayList<JButton> buttonsList;

//'playerO' uses the letter 'O', not the number zero
Icon playerO = new ImageIcon("images/playerO.jpg");
Icon playerX = new ImageIcon("images/playerX.jpg");
Icon playerN = new ImageIcon("images/reset.jpg");

//Instance variable to determine player turn
boolean firstPlayer = true;

//Constructor
public Board()
{
    //Title of Frame
    super("Gui7 - TicTacToe || Jose Reyes");

    //Created container and set the layout
    Container c = getContentPane();
    c.setLayout(new BorderLayout());

    //Created a panel, and set the layout
    JPanel gridPanel = new JPanel();
    gridPanel.setLayout(new GridLayout(3,3));

    //Created new JButtons and added them to the array list 
    for(int i=0; i<=buttonsList.size(); i++){
        JButton jBut = new JButton(playerN);
        buttonsList.add(jBut);
    }

    //Created reset button and title
    reset = new JButton("Play Again?");

    //Added buttons to panel with for loop
    for(int i=0; i<=buttonsList.size(); i++){
        gridPanel.add(buttonsList.get(i));
    }

    //Added panel and reset button to container
    c.add(gridPanel);
    c.add(reset, BorderLayout.PAGE_END);

    //Added Action Listeners with loop
    for(int i=0; i<=buttonsList.size(); i++){
        buttonsList.get(i).addActionListener(this);
    }

    //Added action listener to reset button
    reset.addActionListener(this);

    //Set the window size and set visibility
    setSize(600,600);
    setVisible(true);
}

//ActionEvents
public void actionPerformed(ActionEvent e)
{
    //Grab source
    Object src = e.getSource();

    for (int i = 0; i<=buttonsList.size(); i++){
        if(src == buttonsList.get(i)){
            if(firstPlayer){
                buttonsList.get(i).setIcon(playerO);
                firstPlayer = false;
            } else {
                buttonsList.get(i).setIcon(playerX);
                firstPlayer = true;
            }
        }
    }


    //Reset button icons with loop
    if(src == reset){
        for (int i = 0; i < 9; i++){
            buttonsList.get(i).setIcon(playerN);
        }

        firstPlayer = true;
    }

}

//Main method
public static void main (String args[])
    {
        Board t = new Board();
        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

2 个答案:

答案 0 :(得分:1)

您永远不会为buttonList创建null实例,只需创建一个空(ArrayList)变量引用即可。

更改此变量分配以创建private ArrayList<JButton> buttonsList = new ArrayList<JButton>(); 的实例。

//Created new JButtons and added them to the array list 
for(int i=0; i<=buttonsList.size(); i++){
    JButton jBut = new JButton(playerN);
    buttonsList.add(jBut);
}

修改

我相信你会在这里得到一个OutOfMemoryException:

size

对于每次迭代,您将添加一个按钮,并且arraylist int Width = 3; // Number of cells along the x axis int Height = 3; // Number of cells along the y axis GameObject[] Cells = new GameObject[ Width * Height ];  public int CalculateIndex( int x, int y ) { return y * Width + x; } public void CalculateXY(int i, out x, out y) {     x = i % Width;     y = i / Width; } 将无限增长,直到您成为OOM。不知道你到底在想什么,但也许只是再次思考这里的逻辑。

答案 1 :(得分:1)

i

我猜这是你的OutOfMemoryError的来源。 buttonslist.size()永远不会超过f2()因为每次循环都会添加另一个按钮,从而增加尺寸!