好吧,在这里的第一个帖子,所以大喊我,如果我做错了什么,但我有以下代码,尝试是做一个区域建设者的那种交易的一个老学校的圈子泥。然而,我一直得到一些空指针错误。我在谷歌上搜索了几个小时,修改了几个小时,我想是时候试试别的办法了:P
目的是创建一个3D数组来存储房间的实例。如果你熟悉mud的房间有一个词缀列表,特别是这个房间有一个三维网格,用于每个单独房间的坐标,为图形传递艺术标签。然后,该数组将用于在JPanel上显示每个高度的楼层二维网格,并根据房间的出口状态显示标签图片。问题似乎是a.构造函数没有设置我的变量,这似乎不太可能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)
您的数组充满了空,因为您已经注释掉了您用Room对象填充的位
问题是:您初始化了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();
}
}
}
顺便说一下,你有一个类似的代码,但是被注释了。请确保取消注释或使用提供的代码。