我正在使用 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>