好吧,第一篇文章在这里对我大吼大叫,如果我做错了什么,但是我有以下代码,试图让区域建设者为那些旧学校圈子之一做点交易。但是我一直得到一些空指针错误。我已经谷歌搜索并修改了几个小时这个问题,我觉得是时候尝试一下其他东西:P
目的是创建一个3D数组来存储房间的实例。如果你熟悉的泥房有一个词缀列表,这个特别是有一个三维网格,每个房间的坐标通过艺术标签的图形。然后,阵列将用于在JPanel上显示每个高度的地板的2D网格,其中标签图片基于房间的退出状态。问题似乎是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)
答案 0 :(得分:1)
你的数组已经满了空,因为你已经注释了用Room对象填充它的位。
答案 1 :(得分: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();
}
}
}
顺便说一下,你有一个类似的代码但是被评论了。确保取消注释或使用提供的代码。