gpt4 book ai didi

多维数组中的Java空指针异常

转载 作者:行者123 更新时间:2023-12-01 14:33:44 24 4
gpt4 key购买 nike

好吧,这是这里的第一篇文章,所以如果我做错了什么,请对我大喊大叫,但我有以下代码,尝试是为那些旧学校圈泥之一做一个区域构建器交易。但是我不断收到一些空指针错误。我已经在这个问题上搜索并修改了几个小时,我认为是时候尝试其他方法了: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)

最佳答案

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

关于多维数组中的Java空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16660174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com