谁能告诉我这段代码如何生成一个空文档,但不抛出异常?
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader("<?xml version=\"1.0\"?><company>test</company>")));
起初:
private Document load(String message) throws MessagingException, IOException {
Document document = null;
try {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(message.substring(message.indexOf(XML_BEGIN),
message.indexOf(XML_END)))));
}
catch (Exception e) {
e.printStackTrace();
}
...
Element element = document.getElementById(TIME_STAMP);
结果是:
java.lang.NullPointerException
at ...view.MailReceiver.parse(MailReceiver.java:156)
at ...view.MailReceiver.notify(MailReceiver.java:99)
at ...view.MailReceiver.main(MailReceiver.java:72)