1

I am filling an array of rooms with room objects and creatures into their respective rooms from an xml file using saxParser.

I having trouble figuring out why I seem to be losing everything in the array once I leave my conditional statements in my overridden startElement method. I have two print statements one inside at the bottom of the conditionals and the other outside the conditionals to check if everything is in the array at certain spots. The statement inside the conditionals prints out the correct room name at rooms[0] which is "Violet" and the statement outside the conditionals gives me a nullPointerException and my program crashes.

I feel like I am not setting the array at the end where I should but I cannot catch my mistake. I really appreciate any help I can get.

Here is my class and the startElement method:

    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());
        }}

Here is my room class:

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;
        }


    }

And here is a sample of my input file:

<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>
Richard Miskin
  • 1,260
  • 7
  • 12
Branbron
  • 101
  • 4
  • 12

1 Answers1

0

If tagname is not room then not even the first element will be populated.

This code also looks strange

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")));
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • what I am attempting to do here is I am creating a new room and getting the parameters for the room from the xml file that I am parsing, then assigning it to the current spot in the array rooms[j] then updating my room pointer to the current room which is "current" – Branbron Mar 14 '14 at 01:20