我正在尝试为学校设计一个非常简单的模拟文明游戏。现在我正在努力做到这一点,所以当你点击控制栏上的这个按钮时,你会创建一个新的小屋实例。我将在下面放置错误报告和发生错误的代码。
错误报告:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at controls.Sidebar.clickBuildHut(Sidebar.java:102)
at screen_window.Game_Window.mouseClicked(Game_Window.java:248)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
发生错误的代码:
public void newHut() {
numOfHuts++;
hut[numOfHuts] = new Hut(mouseX, mouseY, 0, numOfHuts);
}
类顶部的小屋声明:
Hut[] hut = new Hut[9999];
int numOfHuts;
Sidebar.clickBuildHut 方法:
public boolean clickBuildHut(int mouseX, int mouseY){
if (viewConstruction){
int topLeftX = x + 2;
int topLeftY = y + 50 - 14;
int selectionWidth = width - 10;
int selectionHeight = 17;
for (int i = 0; i <= width; i++) {
for (int h = 0; h <= height; h++) {
if (mouseX == topLeftX + i && mouseY == topLeftY + h) {
first_map.setEditMode(true);
game_window.newHut();
return true;
}
}
}
}
return false;
}
first_map.setEditMode:
public void setEditMode(boolean editMode) {
this.editMode = editMode;
}
尽管我认为这不是问题,因为当它切换为 true 时,我看到图片正确更改。如果你想让我给你更多关于这方面的信息,我可以但是,我认为它没有任何用处,因为它不是问题所在。