初始化上下文时,我正在尝试将XML文件解析为DOM。但是我收到以下错误:
严重:将上下文初始化事件发送给侦听器的异常 类listener.DomListener的实例java.lang.NullPointerException
这是我的代码
public class DomListener implements ServletContextListener {
private final String FILEPATH = "WEB-INF/studentAccounts.xml";
private ServletContext context;
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Context Initialized");
context = sce.getServletContext();
try {
String realPath = context.getRealPath("/");
String filePath = realPath + FILEPATH;
Document doc = parseDomFromFile(filePath);
context.setAttribute("DOC", (Object) doc);
} catch (ParserConfigurationException ex) {
Logger.getLogger(DomListener.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(DomListener.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(DomListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Context Destroyed");
}
}
我在NullPointerException
行上有一个try{
,但我不知道为什么会发生:(任何人都可以帮忙吗?