简体   繁体   English

主类中其他类的调用方法

[英]Calling method from other class in main

I've set up a method in a Location class to parse an xml file. 我已经在Location类中设置了一种方法来解析xml文件。 But when I try to invoke the method from the main class within the main method, it doesn't seem to be called. 但是,当我尝试从main方法中的main类调用该方法时,似乎没有调用它。

I set a break point on locObj.parseNetwork(); 我在locObj.parseNetwork();上设置了一个断点locObj.parseNetwork(); but it never gets fired, the println's after it execute so not sure what the issue could be. 但它永远不会被触发,因为println是在执行后执行的,所以不确定是什么问题。

Does anyone know why the parseNetwork isn't being called? 有谁知道为什么不调用parseNetwork

This is how I call the method from main: 这就是我从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..");

    }      
}

And this is the actual method within the Location class, both classes are in the same package: 这是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");

    }



}

Added the println before the method call and it gets output, but method still doesn't seem to be called: 在方法调用之前添加了println,并获得输出,但方法似乎仍未被调用:

产量

Given that your above code should successfully call parseNetwork() I imagine you want to check where you are putting your break point? 鉴于您上面的代码应成功调用parseNetwork()我想您想检查一下放置断点的位置? Alternatively put some output in the parseNetwork() and see if it gets printed out. 或者,将一些输出放在parseNetwork() ,看看是否将其打印出来。

It isn't due to any exceptions being thrown during execution as it would fail to print the lines after that method call since you are not handling the exceptions thrown by parseNetwork() 这不是由于在执行过程中引发了任何异常,因为在此方法调用之后它将无法打印行,因为您不处理parseNetwork()引发的异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM