JAVA-嗨,我正在编写一个扫雷程序(第一个大人物),我真的被卡住了。根据我要遵循的规范,程序本身由 2 个类(一个用于逻辑,一个用于 GUI)组成。我在这两门课上都做了很多,但也没有完成。但是,我正在尝试测试从一个类到另一个类的实现调用方法,这就是我卡住的地方。从 GUI 类中,每次用户单击框时,我都尝试在逻辑类中调用方法 openCell(int x, int y)。该逻辑类方法将依次检查方块上是否有地雷、0 或数字,并从 GUI 类调用适当的方法。报错涉及的两种方法如下:
GUI CLASS
public void mouseClicked(MouseEvent e) {
for (int x = 0 ; x < width ; x++) {
for (int y = 0 ; y < height ; y++) {
if (e.getSource() == table[x][y]) {
if(e.getButton() == e.BUTTON1) {
MinesweeperLogic logicClass = new MinesweeperLogic();
logicClass.isOpen(x, y); // <--------------------------- ERROR
}}}}}
LOGIC CLASS
boolean openCell(int x, int y) {
isClicked[x][y] = true;
if(mine[x][y] == true && flag[x][y]==false) {
return false;
} else if(neighborBombs(x, y) > 0 && flag[x][y]==false) {
return true;
}else {
marked = true;
return marked;
}}
以下是当用户点击游戏中的一个框时我收到的错误报告(它在编译代码时会捕获它):
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MinesweeperLogic.isOpen(MinesweeperLogic.java:117)
at MineSweeperGUI.mouseClicked(MineSweeperGUI.java:126)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.Component.processMouseEvent(Component.java:6266)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
最后,如果需要的话,到目前为止两个类的整个(此时不完整)代码。(逻辑类中的方法需要按照项目的指令执行特定的操作,并且在逻辑类中必须没有用户交互)。我无法弄清楚究竟是什么导致了错误。任何指导将不胜感激。(希望这不是很明显的事情,因为即使我得了流感,我也花了过去几个小时试图弄清楚这一点!哈哈)。
import java.awt.* ;
import java.awt.event.* ;
import java.awt.geom.* ;
import javax.swing.* ;
import javax.swing.event.* ;
public class MineSweeperGUI extends JFrame implements ActionListener, MouseListener {
int width = 10;
int height = 10;
JPanel p = new JPanel();
JButton[][] table = new JButton[width][height];
public void MineSweeper() {
MinesweeperLogic logicClass = new MinesweeperLogic();
logicClass.startNewGame(width, height);
JButton[] button = new JButton[width*height];
GridLayout layout = new GridLayout (width, height) ;
p.setLayout(layout);
for(int x = 0 ; x < width ; x++) {
for(int y = 0 ; y < height ; y++) {
table[x][y] = new JButton();
table[x][y].setPreferredSize(new Dimension(25,25));
table[x][y].addMouseListener (this);
p.add(table [x] [y]);
}
}
this.add(p);
this.pack();
this.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
for (int x = 0 ; x < width ; x++) {
for (int y = 0 ; y < height ; y++) {
if (e.getSource() == table[x][y]) {
if(e.getButton() == e.BUTTON1) {
MinesweeperLogic logicClass = new MinesweeperLogic();
logicClass.isOpen(x, y); //<--------------------------------------
}
}
}
}
}
public void gameover(int x, int y) {
table[x][y].setText("*");
}
public static void main(String[]args) {
MineSweeperGUI guiClass = new MineSweeperGUI();
guiClass.MineSweeper();
}}
public void actionPerformed(ActionEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public class MinesweeperLogic {
private int w, h, maxBombs, bombsremaining;
public int width, height;
private boolean mine[][];
private boolean flag[][];
private boolean isClicked[][];
private boolean isZero[][];
private boolean marked;
public void startNewGame(int width, int height) {
w = width;
h = height;
flag = new boolean[w][h];
isZero = new boolean[w][h];
isClicked = new boolean[w][h];
mine = new boolean[w][h];
maxBombs =(int) Math.floor (width*height*0.15);
bombsremaining = maxBombs;
for(int i = 0; i < maxBombs; i++) {
int x = (int) (Math.random() * (w));
int y = (int) (Math.random() * (h));
if (mine[x][y] == false) {
mine[x][y] = true;
isClicked[x][y] = false;
flag[x][y] = false;
}
}
}
int getWidth() {
return w;
}
int getHeight() {
return h;
}
boolean openCell(int x, int y) { // <---------------------------------------------
//MineSweeperGUI guiClass = new MineSweeperGUI();
isClicked[x][y] = true;
if(mine[x][y] == true && flag[x][y]==false) {
//guiClass.gameover(x, y);
return false;
} else if(neighborBombs(x, y) > 0 && flag[x][y]==false) {
return true;
} else {
marked = true;
return marked;
}}
boolean markCell(int x, int y) {
if(flag[x][y] == true) {
flag[x][y] = false;
isClicked[x][y] = false;
bombsremaining++;
marked = false;
return marked;
} else {
flag[x][y] = true;
isClicked[x][y] = true;
bombsremaining--;
if(mine[x][y]==true) {
return true;
} else {
return false;
}
}
}
boolean isOpen(int x, int y) {
if(isClicked[x][y] == false) {
return false;
} else {
return true;
}
}
boolean isMarked(int x, int y) {
if(flag[x][y] == true) {
return true;
} else {
return false;
}
}
int getValue(int x, int y) {
if(mine[x][y] == true) {
return -1;
} else {
return neighborBombs(x, y);
}
}
private int neighborBombs(int x, int y) { // checks surrounding 8 squares for number of bombs
int surBombs = 0;
for (int q = x - 1 ; q <= x + 1 ; q++) {
for (int w = y - 1 ; w <= y + 1 ; w++) {
while (true) {
if (q < 0 || w < 0 || q >= w || w >= h) {
break;
}
if (mine[q][w] == true) {
surBombs++;
break;
}
}
}
}
return surBombs;
}
}