1

saxParser를 사용하여 xml 파일에서 룸 객체와 생물이있는 방의 배열을 각 방에 채 웁니다.자바 채우기에서 nullPointerException saxParser를 사용하여 계층 구조

일단 내가 재정의 한 startElement 메소드에 내 조건문을 남겨두면 왜 배열에서 모든 것을 잃어 버리고있는 것일까를 알아 내는데 어려움을 겪고 있습니다. 나는 조건문의 하단에 두 개의 print 문을 가지고 있고 조건의 바깥쪽에는 다른 문이 하나있다. 모든 것이 특정 지점의 배열에 있는지 확인한다. 조건문 내부의 문은 rooms [0]에서 올바른 방 이름을 "Violet"으로 출력하고 조건문 외부 문은 nullPointerException을 발생시키고 프로그램이 중단됩니다.

필자는 필연적으로 필자가 끝까지 배열을 설정하지 않고 있지만 내 실수를 잡지 못합니다. 내가 얻을 수있는 도움에 정말 감사드립니다. 여기

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

내 방 클래스입니다 : 여기

내 클래스와의 startElement 방법

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> 
+0

당신의'Room' 클래스를 보여주십시오 이상한 보인다. 그리고 어떻게이 코드를 호출하고 있습니다. –

+0

견본 입력 XML을 보여 주시면 더 잘 도와 드리겠습니다. – Alvin

+0

XML에서 태그를 가져 와서 올바른 방을 만들려고합니다 saxparser를 사용하여 올바른 이웃들과 올바른 생물을 추가하고 해당 객체를 만듭니다 – Branbron

답변

0

tagnameroom이 아니면 첫 번째 요소조차 채워지지 않습니다.

이 코드는

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"))); 
+0

내가 여기서하려고하는 것은 새 방을 만들고 파싱하는 XML 파일에서 방의 매개 변수를 가져 오는 것입니다. 배열 실 (Array Room) [j]의 현재 위치에 할당 한 다음 방 방을 "현재"방의 방으로 업데이트합니다. – Branbron