gpt4 book ai didi

java - 线程 :Snake game 中出现异常

转载 作者:行者123 更新时间:2023-12-02 05:15:24 25 4
gpt4 key购买 nike

我正在创建一个贪吃蛇游戏我使用二维数组作为我的背框。没有语法错误。我在线程中收到异常。这就是我认为错误所在:

 java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new SnakeGame().setVisible(true);
SnakeGame obj1;
obj1= new SnakeGame();
obj1.gameEngine();
}

case 908:
x9y8= new JLabel(icon);
break;
case 9:
x0y9= new JLabel(icon);
break;

我正在使用 netbeans 并使用调色板创建标签。因此标签已经创建。异常(exception)是:

运行:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at snakegamex.SnakeGame.<init>(SnakeGame.java:775)
at snakegamex.SnakeGame$1.run(SnakeGame.java:744)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 4 seconds)

错误是什么?我应该如何纠正它?错误应该出现在第 775 行

int score=0;
int n=5,c=7;
ImageIcon image= new ImageIcon(getClass().getResource("1327691176237.jpg"));
ImageIcon image1= new ImageIcon(getClass().getResource("1327691176397.jpg"));

我已将图像导入到包中,并且还创建了默认包

最佳答案

异常发生在类构造函数内的 SnakeGame 类的第 775 行 - 看起来您正在尝试创建一个 ImageIcon 对象,并传入 null引用而不是一些图像数据。

关于java - 线程 :Snake game 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26994707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com