Java 添加ActionListener时数组中出现空指针异常
为了一个学校项目,我一直在做一个扫雷游戏。它本质上是游戏的克隆,但是现在,当我尝试为JButtons添加动作侦听器时,我得到了一个Java 添加ActionListener时数组中出现空指针异常,java,swing,nullpointerexception,jbutton,actionlistener,Java,Swing,Nullpointerexception,Jbutton,Actionlistener,为了一个学校项目,我一直在做一个扫雷游戏。它本质上是游戏的克隆,但是现在,当我尝试为JButtons添加动作侦听器时,我得到了一个nullpointerexception。有什么帮助吗?这是我的密码: import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JOptionPane; import java.awt.event.ActionListener; imp
nullpointerexception
。有什么帮助吗?这是我的密码:
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.util.Random;
public class Grid {
public static int c, d; //Necessary for the allowance of usage within the listeners from lines 47 - 68.
JFrame frame = new JFrame();
public static boolean[][] isBomb;
public static int bombProbability;
public static JButton[][] grid;
Random Bomb = new Random();
public Grid(int width, int length){
bombProbability = (int) Double.parseDouble(JOptionPane.showInputDialog("Input the likeliness of a bomb:"));
frame.setLayout(new GridLayout(width, length));
grid = new JButton[width][length];
isBomb = new boolean[width + 2][length + 2];
for(int a = 1; a <= length; a++){
for(int b = 1; b <= width; b++){
grid[a][b] = new JButton();
frame.add(grid[a][b]);
if((Bomb.nextInt(99) + 1) <= bombProbability){
isBomb[a][b] = true;
grid[a][b].setText(String.valueOf(isBomb[a][b])); //Delete this before final product
}
}
}
frame.setTitle("Minesweeper!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(500, 500));
frame.pack();
frame.setVisible(true);
for(c = 0; c < length; c++){
for(d = 0; d < width; d++){
grid[c][d].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(isBomb[c][d] == true){
JOptionPane.showMessageDialog(null, "BOOM! You're DEAD!");
System.exit(0);
}
else{
indexCells(c,d);
}
}
});
}
}
}
public static void indexCells(int c,int d){
int[][] nearbyBombs = new int[c+2][d+2];
for (int i = 1; i <= c; i++){
for (int j = 1; j <= d; j++){
// (ii, jj) indexes neighboring cells
for (int ii = i - 1; ii <= i + 1; ii++){
for (int jj = j - 1; jj <= j + 1; jj++){
if (isBomb[ii][jj]){
nearbyBombs[i][j]++;
}
}
}
grid[i][j].setText(String.valueOf(nearbyBombs[i][j]));
if(nearbyBombs[i][j] == 0){
for(int iii = i - 1; iii <= i + 1; iii ++){
for(int jjj = j - 1; jjj <= j + 1; jjj ++){
indexCells(iii,jjj);
}
}
}
}
}
}
public static void main(String []args){
//int columns = (int) Double.parseDouble(JOptionPane.showInputDialog(null, "Input the number of columns:"));
//int rows = (int) Double.parseDouble(JOptionPane.showInputDialog(null, "Input the number of rows:"));
new Grid(10, 10);
}
}
import javax.swing.JFrame;
导入javax.swing.JButton;
导入javax.swing.JOptionPane;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.awt.GridLayout;
导入java.awt.Dimension;
导入java.util.Random;
公共类网格{
publicstatic int c,d;//第47-68行的侦听器中允许使用必需的。
JFrame=新JFrame();
公共静态布尔值[][]isBomb;
公共静态概率;
公共静态JButton[][]网格;
随机炸弹=新随机炸弹();
公共网格(整数宽度、整数长度){
bombProbability=(int)Double.parseDouble(JOptionPane.showInputDialog(“输入炸弹的相似性:”);
frame.setLayout(新的网格布局(宽度、长度));
网格=新的JButton[宽度][长度];
isBomb=新布尔值[宽度+2][长度+2];
对于(inta=1;a,首先要这样做
for(int a = 1; a <= length; a++){
for(int b = 1; b <= width; b++){
grid[a][b] = new JButton();
for(c = 0; c < length; c++){
for(d = 0; d < width; d++){
grid[c][d].addActionListener(new ActionListener() {
为什么在这里(inta=1;a)它在线程“main”中给出了这个错误异常java.lang.ArrayIndexOutOfBoundsException:10?我以前就知道了,但这个版本的代码中没有。我不知道这是怎么发生的。它给了我一个错误,我告诉你我看到了这个错误的问题。在第30行,当网格的维度设置为宽度和长度时,两者都应该是+2。我不知道为什么这不是正确的你已经知道了。
for(int a = 0; a < length; a++){
for(int b = 0; b < width; b++){
grid[a][b] = new JButton();