0

好吧,第一次在这里发帖,所以如果我做错了什么就对我大喊大叫,但我有以下代码,试图为那些老派圈泥中的一个做一个区域建设者的交易。但是我不断收到一些空指针错误。我已经在这个问题上搜索和修改了几个小时,我认为是时候尝试一下其他东西了:P

目的是创建一个 3D 数组来存储房间实例。如果你熟悉泥的房间有一个词缀列表,特别是这个有一个三维网格,用于每个房间的坐标,传递图形的艺术标签。然后,该数组将用于在 JPanel 上显示每个高度的楼层的 2D 网格,并根据房间的退出状态使用标签图片。问题似乎是,要么。构造函数没有设置我的变量,这似乎不太可能 b。我用错了(很可能是因为我对java并不熟悉)或c。我忽略了一些非常容易的错误。

每当我尝试访问 Rooms 类中的变量时,就会出现错误。

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));
            }
        }
    }*/
}
}

错误列表:

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)
4

2 回答 2

1

您的数组充满了空值,因为您已经注释掉了用 Room 对象填充它的位。

于 2013-05-21T00:16:28.017 回答
0

这就是问题所在:您初始化了Rooms[][][] a数组,但从未初始化其中的任何值。因此,这条线的执行

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

结果是一个NullPointerException

确保初始化数组中的元素。基本示例:

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

顺便说一句,您有类似的代码,但已被注释。确保取消注释或使用提供的代码。

于 2013-05-21T00:16:53.037 回答