Daynamically Web应用程序,我创build文件夹中的Excel文件,但我得到NPE

我用jsp / servlet创build了一个dynamic的web应用程序。 我使用Excel文件(udaanmasterdatabase.xls)来存储数据库等数据。

Excel文件(udaanmasterdatabase.xls)放在源文件(src文件)中,我可以读取数据。 我的问题是,当我运行时,我不能在src文件夹中的Excel文件。 有人可以解释我为什么不能创build这个Excel文件吗?

// firstly i putting the excel file in src\com\roomantecheducation\excelsave folder then i mapping in web.xml <context-param> <param-name>MyAppHome</param-name> <param-value>com\roomantecheducation\excelsave\</param-value> </context-param> <listener> <listener-class>com.roomantecheducation.controller.MyServletContextListener</listener-class> </listener> 

而且servlet,

 // after that i putting the code as public class MyServletContextListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent arg0) { ServletContext ctx = arg0.getServletContext(); String filePath = ctx.getRealPath("") + File.separator+ctx.getInitParameter("MyAppHome")+"udaanmasterdatabase.xls"; System.out.println(filePath); ctx.setAttribute("filePath", filePath); } } /*then excel file create and data inserted in excel file i getting the null pointer exception here. but i give static path like // FileOutputStream out = new FileOutputStream(new File("G:\\INVENTORY\\INDGLOBAL2\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\Roomantechnologyeducation1\\WEB-INF\\classes\\com\\roomantecheducation\\excelsave\\udaanmasterdatabase.xls")); then excel file create but dynamically set in the fileOutputStream it getting error */ ExcelSheetController.java try { String filepath = (String)getServletContext().getAttribute("filePath"); FileOutputStream out = new FileOutputStream(new File(filepath)); workbook.write(out); out.close(); System.out.println("Excel written successfully.."); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }