我正在为 PC 创建一个 Breakout 的克隆,今晚早些时候我发现有一个 NullPointerException 被抛出并显示在控制台中。奇怪的是,该方法run()
被告知要捕获发生的任何异常并显示其堆栈跟踪,然后忽略它们,但这一次它在开始时冻结游戏,在日志中,按键仍然被跟踪,但游戏中没有任何反应。显然,游戏并没有中止,而是不会重新绘制。
它似乎来自一行代码:
if (by >= bricks[0].y && by <= bricks[0].y + bricks[0].img.getHeight(null) && bx >= bricks[0].x && bx <= bricks[0].x + bricks[0].img.getWidth(null)) {
这是日志和堆栈跟踪(整个事情):
Game starting, menu boot.
Menu Loaded, JFrame visible.
Running thread.
Play clicked, start game initialization.
Creating JFrame.
JFrame created and visible.
Starting game thread
Initialize user variables.
Initialized user variables.
Initialization complete, start game.
java.lang.Execption in line 86/87, class Game. Stacktrace:
java.lang.NullPointerException
at Game.move(Game.java:195)
at Game.run(Game.java:86)
at java.lang.Thread.run(Thread.java:722)
Space hit!
Key ARROWLEFT hit.
Key ARROWLEFT hit.
Key ARROWLEFT hit.
Key ARROWLEFT hit.
Key ARROWLEFT hit.
Key ARROWLEFT hit.
Key ARROWLEFT hit.
Key ARROWLEFT released.
Key ESCAPE Hit!
我希望这个问题可以得到解决。