- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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/
我遇到过这个 html: 上面的html和这个有什么区别: 最佳答案 来自MDN page on the tag : 对于 type 的属性标签,可能的值是: 提交:按钮将表单数据提交给服务器
Button button= (Button) findViewbyID(R.id.button); 和 Button button = new Button(this); 有什么区别? 最佳答案 有
我是一名优秀的程序员,十分优秀!