gpt4 book ai didi

Java错误: TicTacToe game with arrayList for buttons

转载 作者:行者123 更新时间:2023-12-02 03:01:59 25 4
gpt4 key购买 nike

我有一个 tictactoe 程序,我想通过对按钮和 Action 监听器使用 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);
}
}

最佳答案

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

更改此变量分配以创建 ArrayList 的实例。

private ArrayList<JButton> buttonsList = new ArrayList<JButton>();

编辑

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

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

对于每次迭代,您将添加一个按钮,并且 arraylist size 将无限增长,直到 OOM。不知道你到底在尝试什么,但也许只是再次思考一下这里的逻辑。

关于Java错误: TicTacToe game with arrayList for buttons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42322613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com