-3

I'm trying to design a pretty simple simulation civilization game for school. Right now I'm trying to make it so when you click this button on the control bar you make a new hut instance. I will put error report below and code where the error occurs.

Error Report:

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)

Code where error occurs:

public void newHut() {
        numOfHuts++;
        hut[numOfHuts] = new Hut(mouseX, mouseY, 0, numOfHuts);
}

Hut declaration at top of class:

Hut[] hut = new Hut[9999];
int numOfHuts;

Sidebar.clickBuildHut method:

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;
}

Although I don't think that this is the problem because when this is switched to true I see the picture change correctly. If you want me to give you more information on this I can but, I don't think that it would be of any use since it isn't where the problem lies.

Liam15
  • 75
  • 1
  • 6

2 Answers2

0

I don't see any place that you iniailize the hut array. You've declared it, but you haven't initialized it, so Java defaults it to null.

Somewhere, you need to initialize it with something like this:

hut = new Hut[size];

perhaps in a constructor.

EDIT

Based on the question edit that now displays the clickBuildHut method, depending on which line is actually line 102, either first_map or game_window is null.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • I changed my hut declaration to "Hut[] hut= new Hut[9999];" but I'm still receiving the same error. I also tried seperating it so it is declared "Hut[] hut;" then later it is declared "hut = new Hut[9999]". – Liam15 Mar 28 '13 at 23:54
  • I added the method for first_map and explained why I don't think that, that is the problem. – Liam15 Mar 29 '13 at 00:22
0

Locate line 102 of Sidebar.java. Based on the stack trace that is the location of the NPE. By your code postings I would guess line 102 is either:

first_map.setEditMode(true);

or

game_window.newHut();

I say that because those are the only object references in the clickBuildHut method. So, either first_map is null or game_window is null.

Brent Worden
  • 10,624
  • 7
  • 52
  • 57