Question

Alrighty, First post here so yell at me if I'm doing something wrong but I have the following code, the attempt is to make a zone builder kind of deal for one of those old school circle mud's. However I keep getting some null pointer errors. I've googled and modified for hours on this problem and I figure it's time to give something else a try :P

The intent is to create a 3D array to store instances of rooms. If your familiar with mud's rooms have a list of affixes and this one in particular has a three dimensional grid for coordinates of each individual room passing art tags for graphics. The array would then be used to display a 2D grid of a floor for each altitude on the JPanel with the label pictures based on exit status of the room. The problem seems to be that either a. the constructor is not setting my variables which seems unlikely b. i'm using it wrong (quite likely as I'm not real familiar with java) or c. I'm overlooking some really easy mistake.

The errors come any time I try to access a variable within the Rooms class.

package battle.org;

public class GUI extends JFrame {

public class Rooms{
    String Rt;
    String desc;
    boolean dark;     
    boolean indoors;    
    boolean nomob;      
    boolean peace;      
    boolean soundproof;
    boolean notrack;
    boolean nomag;
    boolean tunnel;
    boolean notify;     
    boolean SF;         
    boolean FF;         
    boolean noport;     
    boolean noquit;     
    boolean exitu;      
    boolean exitd;      
    boolean onmap;      
    boolean exitud;     
    boolean exite;      
    boolean exitw;      
    boolean exits;     
    boolean exitn;

    public Rooms(){
        String Rt   = "Room Name";
        String desc = "Default Description";
        dark       = false;
        indoors    = false;
        nomob      = false;
        peace      = false;
        soundproof = false;
        notrack    = false;
        nomag      = false;
        tunnel     = false;
        notify     = false;
        SF         = false;
        FF         = false;
        noport     = false;
        noquit     = false;
        exitu      = false;
        exitd      = false;
        onmap      = false;
        exitud     = false;
        exite      = false;
        exitw      = false;
        exits      = false;
        exitn      = false;
    }       
}

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GUI frame = new GUI();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public GUI() {

    final int MAXxy = 10;
    final int MAXz  = 1;
    int z;
    Rooms[][][] a = new Rooms[MAXxy][MAXxy][MAXz];
//  for(int x = 0; x < MAXxy; x++){
//      for(int y = 0; y < MAXxy ; y++){
//          for(z = 0; z < MAXz; z++){
//              a[x][y][z] = new Rooms(); 
//          }
//      }
//  }

//Labels to be displayed in grid
JLabel room = new JLabel(new ImageIcon("C:\\Users\\Owner\\Desktop\\room.jpg"));
JLabel nothing = new JLabel(new ImageIcon("C:\\Users\\Owner\\Desktop\\empty.jpg"));
JLabel up = new JLabel(new ImageIcon("C:\\Users\\Owner\\Desktop\\up.jpg"));
JLabel down = new JLabel(new ImageIcon("C:\\Users\\Owner\\Desktop\\down.jpg"));


            //Error here wont let me access the boolean
    a[0][0][0].onmap = true;
    int x, y;
    x =0; y =0; z = 0;
    if(a[0][0][0].onmap){
        System.out.print("X = " + x + " Y = " + y + " Z = " + z + "");
        contentPane.add(down,  new GridBagConstraints(x, y, 1, 1, 0.0, 0.0 ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    }


    //Displays Altitude level z
    z = 0;
/*  for(int x = 0; x < MAXxy; x++){
        for(int y = 0; y < MAXxy ; y++){
            if(a[0][0][0].onmap){
                contentPane.add(down,  new GridBagConstraints(x, y, 1, 1, 0.0, 0.0 ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
            }
        }
    }*/
}
}

List of errors:

java.lang.NullPointerException
    at battle.org.GUI.<init>(GUI.java:189)
    at battle.org.GUI$1.run(GUI.java:77)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(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.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)
Was it helpful?

Solution

Your array is full of nulls because you have commented out the bit where you fill it with Room objects.

OTHER TIPS

Here's the problem: you initialize the Rooms[][][] a array but never initialize any value in it. Thus, the execution of this line

a[0][0][0].onmap = true;

results in a NullPointerException.

Make sure to initialize the elements in the array. Basic example:

for(int i = 0; i < MAXxy;i++) {
    for(int j = 0; j < MAXxy; j++) {
        for(int k = 0; k < MAXz; k++) {
            a[i][j][k] = new Rooms();
        }
    }
}

By the way, you have a similar code but is commented. Make sure to uncomment it or to use provided code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top