gpt4 book ai didi

使用 saxParser 填充对象层次结构时出现 Java nullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 15:02:35 27 4
gpt4 key购买 nike

我正在使用 saxParser 从 xml 文件将房间对象和生物填充到各自的房间中。

当我将条件语句留在重写的 startElement 方法中时,我很难弄清楚为什么我似乎会丢失数组中的所有内容。我有两个打印语句,一个位于条件语句的底部,另一个位于条件语句的外部,以检查所有内容是否都在数组中的某些位置。条件语句内的语句在 rooms[0] 处打印出正确的房间名称,即“Violet”,条件语句外的语句给出 nullPointerException,并且我的程序崩溃。

我觉得我没有在应该的位置设置数组,但我无法发现我的错误。我真的很感谢我能得到的任何帮助。

这是我的类和 startElement 方法:

    public class MyHandler extends DefaultHandler {

private Room current;
private PC player;
private Room[] rooms = new Room[10];

@Override
public void startElement(String discard1, String discard2, String tagname, Attributes attr) {

if (tagname.equals("room")) {
for (int j = 0; j < rooms.length; j++) {
if (rooms[j] == null) {
current = rooms[j] = new Room(attr.getValue("name"), attr.getValue("description"), attr.getValue("state"), new Room(attr.getValue("north")), new Room(attr.getValue("south")), new Room(attr.getValue("east")), new Room(attr.getValue("west")));
break;
}
}
// System.out.println(current);
} else if (tagname.equals("animal")) {
current.addCreature(new Animal(current, attr.getValue("name"), attr.getValue("description")));
// System.out.println(current);
} else if (tagname.equals("NPC")) {
current.addCreature(new NPC(current, attr.getValue("name"), attr.getValue("description")));
// System.out.println(current);
} else if (tagname.equals("PC")) {
//this means the tag is the PC
player = new PC(current, attr.getValue("name"), attr.getValue("description"), 40);
current.addCreature(player);
System.out.println(rooms[0].getRoomName());
}
System.out.println(rooms[0].getRoomName());
}}

这是我的房间等级:

public class Room {

private String state;
private String roomScript;
private String roomName;
private Creature[] creatures = new Creature[10];
private int top = 0;
private Room north;
private Room south;
private Room east;
private Room west;

public Room(String r, String s, String rS, Room n, Room so, Room e, Room w) {
roomName = r;
roomScript = rS;
state = s;
north = n;
south = so;
east = e;
west = w;
}

public Room(String n){
roomName = n;
}

public String getState(){
return state;
}

public void changeState(String s){
state = s;
}

public String getRoomName(){
return roomName;
}

public void addCreature(Creature a) {

if (top >= 10) {
System.out.println("This room is full.");

} else {
creatures[top] = a;
top++;

}
}
public void removeCreature(Creature a){
for (int i = 0; i < creatures.length; i++) {
if (creatures[i] == null){
System.out.println("Nobody is home");
} else if (creatures[i].equals(a)){
creatures[i] = null;
}
}
}

public void notifyCreatures(Room r){
for (int i = 0; i < creatures.length ; i++) {
creatures[i].react();
}

}




public String toString() {
if (top == 0) {
return roomName + ", contains: nothing";
}
String temp = "";
for (int i = 0; i < top; i++) {

temp = temp + " " + creatures[i].toString();

}
return roomName + ": " + roomScript + " is: " + state + ". Neighbors are: " + north.getRoomName() + ", " + south.getRoomName() + ", " + east.getRoomName() + ", " + west.getRoomName() + ". It contains: " + temp;
}


}

这是我的输入文件的示例:

<xml version="1.0" encoding="UTF-8">
<room name="Violet" description="a large round room with violet draperies"
state="half-dirty" north="Pig" east="Student" south="Teacher" west="Green">
<animal name="Peter" description="a brown dog" />
<animal name="Lily" description="a black and white cat" />
<NPC name="Mary" description="a tall woman" />
<PC name="Squash" description="one who doesn't leave the animals and NPC-s alone" />
</room>
<room name="Pig" description="this one used to be a pigsty, now a guestroom"
state="clean" north="Roofless" south="Violet" west="Windy">
<animal name="Lucy" description="a pink pig" />
<animal name="Mutsu" description="a black-spotted pig" />
</room>
<room name="Student" description="the pigs don't like to come here"
state="dirty" south="Snowy" west="Violet">
<NPC name="Jeremy"
description="a student who writes Java projects in the very last moment" />
<NPC name="Martina" description="a student who doesn't write Java projects at all" />
<NPC name="Lewell" description="a student who doesn't even know what a Java project is" />
<NPC name="Ema" description="a student who always writes Java projects on time" />
</room>
<room name="Teacher" description="the pigs don't like to come here either"
state="dirty" north="Violet" east="Snowy" west="Drafty">
<NPC name="Pencho" description="students always fall asleep in his class" />
<NPC name="Divachka" description="a teacher who falls asleep in her own classes" />
</room>
<room name="Green" description="there is grass growing on the floor"
state="half-dirty" north="Windy" east="Violet" south="Drafty">
<NPC name="Robot" description="made of tin" />
<NPC name="AI" description="made of intelligent parts" />
<animal name="Savage" description="a chihuahua" />
<animal name="Fluffy" description="a rottweiler" />
</room>
<room name="Roofless"
description="it was not designed like this, but both teachers and students are too lazy to repair"
state="half-dirty" east="Basement" south="Pig">
<NPC name="Programmer" description="this one knows how to write Java projects" />
<animal name="Meow" description="this animal is an old hairy lion" />
</room>
<room name="Basement" description="there is mold on the walls"
state="half-dirty" west="Roofless">
<animal name="Roddy" description="a big rat who has his own hole in the basement" />
<NPC name="Moldy" description="likes to sleep in the basement" />
</room>
<room name="Windy" description="the windows are not insulated properly"
state="dirty" east="Pig" south="Green">
<NPC name="Maggie" description="likes animals" />
<NPC name="Carrie" description="likes messy rooms" />
</room>
<room name="Drafty" description="someone needs to patch that wall"
state="clean" north="Green" east="Teacher">
<animal name="Milka" description="a violet cow" />
<animal name="Marko" description="a gray donkey" />
<animal name="Moo" description="a jersey cow" />
</room>
<room name="Snowy"
description="this one is inaccessible November-March if you don't have a shovel"
state="clean" north="Student" west="Teacher">
<animal name="Polly" description="a polar bear" />
<animal name="Pen" description="a penguin" />
</room>
</xml>

最佳答案

如果tagname不是room,那么甚至不会填充第一个元素。

这段代码看起来也很奇怪

current = rooms[j] = new Room(attr.getValue("name"), attr.getValue("description"),
attr.getValue("state"), new Room(attr.getValue("north")),
new Room(attr.getValue("south")), new Room(attr.getValue("east")),
new Room(attr.getValue("west")));

关于使用 saxParser 填充对象层次结构时出现 Java nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22393843/

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