簡體   English   中英

主類中其他類的調用方法

[英]Calling method from other class in main

我已經在Location類中設置了一種方法來解析xml文件。 但是,當我嘗試從main方法中的main類調用該方法時,似乎沒有調用它。

我在locObj.parseNetwork();上設置了一個斷點locObj.parseNetwork(); 但它永遠不會被觸發,因為println是在執行后執行的,所以不確定是什么問題。

有誰知道為什么不調用parseNetwork

這就是我從main調用方法的方式:

public class GrailQuestMain {

    public static void main(String[] args) throws Exception {

        //Parse in the xml file
        Location locObj = new Location();
        locObj.parseNetwork();

        //Start screen prompt
        System.out.println("********************************GRAIL QUEST************************************");
        System.out.println("-------------------------------------------------------------------------------");
        System.out.println("-------------------------------------------------------------------------------");
        System.out.println("Hit enter to begin your quest to Cyprus..");
        new Scanner(System.in).nextLine();
        System.out.println("Loaded..");

    }      
}

這是Location類中的實際方法,兩個類位於同一包中:

public class Location implements Lookable{
    private List<AbstractGameCharacter> observers = new ArrayList<AbstractGameCharacter>();
    private List<Thing> objects = new ArrayList<Thing>();
    private List<Exit> exits = new ArrayList<Exit>();
    private String name;
    private String description;

    public void enter(AbstractGameCharacter gc){
        observers.add(gc);
    }

    public void exit(GameChacter gc){
        observers.remove(gc);
    }


    public void parseNetwork() throws ParserConfigurationException, SAXException, IOException{

         //Get the DOM Builder Factory
        DocumentBuilderFactory factory = 
            DocumentBuilderFactory.newInstance();

        //Get the DOM Builder
        DocumentBuilder builder = factory.newDocumentBuilder();

        //Load and Parse the XML document
        //document contains the complete XML as a Tree.
        Document document = 
          builder.parse(new File("network.xml"));

        NodeList locationName = document.getElementsByTagName("location name");

    }



}

在方法調用之前添加了println,並獲得輸出,但方法似乎仍未被調用:

產量

鑒於您上面的代碼應成功調用parseNetwork()我想您想檢查一下放置斷點的位置? 或者,將一些輸出放在parseNetwork() ,看看是否將其打印出來。

這不是由於在執行過程中引發了任何異常,因為在此方法調用之后它將無法打印行,因為您不處理parseNetwork()引發的異常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM