我在java中创建了一个Minesweeper版本,但是在设置toggleButtons的鼠标监听器时遇到了问题。我需要在左右键单击中执行不同的操作,因此这是我在MyPanel类中设置的内容:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class MyPanel extends JPanel {
JToggleButton jButtons[] = new JToggleButton[100];
int i = 0;
MyFrame frame;
JLabel testOut;
gameEngine engine;
public MyPanel(MyFrame mf) {
for (i = 0; i< 100; i++) {
jButtons[i] = new JToggleButton("");
jButtons[i].addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)) { //**Exception here**
engine.markCell(i);
System.out.println("L-click");
} else if(SwingUtilities.isRightMouseButton(e)) { //**or here**
engine.showCell(i);
System.out.println("R-click");
}
}
});
//System.out.print(i);
add(jButtons[i]);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
然而,我得到了一个&#39; nullPointerException&#39;在其中一条标记线上(取决于L或R-单击):
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MyPanel$1.museReleased(MyPanel.java:31)
at java.AWTEventMulticaster.mouseReleased(Unknown Source)
这似乎就像“鼠标事件”一样。抛出异常,但我不明白为什么会这样,更不用说如何解决它。
以下是我的其他课程,因此您可以看到&#39; MyFrame&#39;和&#39; gameEngine&#39;参考。
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class MyFrame extends JFrame {
JToggleButton jButtons[] = new JToggleButton[100];
gameEngine engine;
MyPanel panel;
public MyFrame() {
setTitle("Minesweeper");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new MyPanel(this);
panel.setLayout(new GridLayout(10,10));
add(panel);
setVisible(true);
setResizable(false);
}
public gameEngine getGame() {
return engine;
}
}
-
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/** model of minesweeper game: contains mine arrays, as well as methods
for left and right click responses.**/
public class gameEngine {
private int[][] mines;
private int Row;
private int Col;
//seed based random number generator
double rnd(){
int seed = 42;
Random gen = new Random(seed);
double output = gen.nextDouble();
return output;
}
/*generate array to represent minefield. generated array will contain only 0s and 1s
possible cases of array are:
0: no mine, unmarked
1: mine, unmarked
2: no mine: marked
3: mine: marked */
//this method generates initial empty array, then calls layMines to populate it
public void initField(){
mines = new int[10][10];
for (int i=0; i<10; i++)
for(int j=0; j<10; j++)
mines[i][j] = 0;
layMines();
}
//lays 16 mines into the array using a random number generator
public void layMines(){
int minesOnField = 0;
for (int i=0; i<10; i++)
for (int j=0; j<10; j++)
if (rnd() >=70) { //if roll is successful
if (minesOnField < 16){ //and less than 16 mines have been
if(mines[i][j] == 0) { //AND it isnt already a mine
minesOnField ++;
mines[i][j] = 1;
}
}
}
}
public void showCell(int x) {
}
public void markCell(int x){
}
public void test() {
initField();
System.out.println(Arrays.deepToString(mines));
}
}
我的主要&#39;方法只需调用&#39; MyFrame。&#39;
答案 0 :(得分:0)
您将gameEngine声明为
gameEngine engine;
虽然您从未初始化它,但您使用它:
if(SwingUtilities.isLeftMouseButton(e)) { //**Exception here**
engine.markCell(i);
...
} else if(SwingUtilities.isRightMouseButton(e)) { //**or here**
engine.showCell(i);
..
默认情况下,如果没有初始化对象,则将对象初始化为null,对null的任何方法调用都将导致NullPointerException。
您应该初始化引擎。