问题描述
在一块静态模块中加载一配置文件,使用main函数测试通过,但在应用中却报错,如下publicstaticStringpartnerID="";static{Propertiespro=newProperties();InputStreaminputStr=Object.class.getResourceAsStream("/pay.properties");try{pro.load(inputStr);partnerID=pro.getProperty("partnerID").trim();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}publicstaticvoidmain(String[]args){System.out.println(partnerID);}使用main没问题,应用报错报错如下:java.lang.NullPointerExceptionatjava.util.Properties$LineReader.readLine(Properties.java:418)atjava.util.Properties.load0(Properties.java:337)atjava.util.Properties.load(Properties.java:325)atcom.alipay.config.AlipayConfig.<clinit>(AlipayConfig.java:58)atorg.apache.jsp.index_jsp._jspService(index_jsp.java:90)atorg.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:803)atorg.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)atorg.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)atorg.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:803)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)atorg.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)atorg.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)atorg.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)atjava.lang.Thread.run(Thread.java:662)哪位大仙帮小弟解决一下!!
解决方案
解决方案二:
哈哈,问题解决了,是因为我用了object的类对象,然后他根据此类位置找被加载文件时路径就不对了。而用main可以通过是因为只要文件在项目中myeclipse都可以找到,其实路径是错的,发布之后就不对了。
解决方案三:
嗯有进步就不错,祝贺..开发环境和部署环境是不一样的,这个要注意下。