简体   繁体   中英

Calling method from other class in main

I've set up a method in a Location class to parse an xml file. But when I try to invoke the method from the main class within the main method, it doesn't seem to be called.

I set a break point on locObj.parseNetwork(); but it never gets fired, the println's after it execute so not sure what the issue could be.

Does anyone know why the parseNetwork isn't being called?

This is how I call the method from 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:

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:

产量

Given that your above code should successfully call parseNetwork() I imagine you want to check where you are putting your break point? Alternatively put some output in the parseNetwork() and see if it gets printed out.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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