[英]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.