-3

我正在尝试为学校设计一个非常简单的模拟文明游戏。现在我正在努力做到这一点,所以当你点击控制栏上的这个按钮时,你会创建一个新的小屋实例。我将在下面放置错误报告和发生错误的代码。

错误报告:

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 时,我看到图片正确更改。如果你想让我给你更多关于这方面的信息,我可以但是,我认为它没有任何用处,因为它不是问题所在。

4

2 回答 2

0

我没有看到您初始化hut阵列的任何地方。你已经声明了它,但你还没有初始化它,所以 Java 将它默认为null.

在某个地方,您需要使用以下内容对其进行初始化:

hut = new Hut[size];

也许在构造函数中。

编辑

根据现在显示该clickBuildHut方法的问题编辑,取决于哪一行实际上是第 102 行,要么first_map要么game_windownull

于 2013-03-28T23:45:55.483 回答
0

找到 Sidebar.java 的第 102 行。基于作为 NPE 位置的堆栈跟踪。根据您的代码发布,我猜第 102 行是:

first_map.setEditMode(true);

或者

game_window.newHut();

我这么说是因为这些是clickBuildHut方法中唯一的对象引用。因此,要么first_map为空,要么为game_window空。

于 2013-03-29T02:44:18.473 回答