我刚将所有的java类移动到一个包(SSP),因为我将它们放在默认包uo中,现在我的应用程序不再部署到tomcat ....
我用:
我尝试了以下内容而没有任何改变:
错误(Tomcat日志):
03-Dec-2015 09:53:13.330 SEVERE [http-nio-8080-exec-4] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class SSP.Startup java.lang.NullPointerException
at SSP.Load_config.getPropValues_db_user_pw(Load_config.java:89)
at SSP.Roles_rights.get_roles_rights(Roles_rights.java:52)
at SSP.Roles_rights.getInstance(Roles_rights.java:35)
at SSP.Startup.contextInitialized(Startup.java:21)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4738)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5181)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:586)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:460)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1586)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1460)
at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:906)
at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:344)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
错误Netbeans调试:
FAIL - Deployed application at context path /Self-Service-Portal but context failed to start
D:\Dropbox\Bachelor\Java Projects\Self-Service-Portal\nbproject\build-impl.xml:1157: The module has not been deployed.
以下代码片段是tomcat日志中指定的类:
SSP.Load_config.getPropValues_db_user_pw(Load_config.java:89)
72: public String getPropValues_db_user_pw() throws IOException {
73: try {
74: Properties prop = new Properties();
75: String propFileName = "db_settings.properties";
76: inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
77
78: if (inputStream != null) {
79: prop.load(inputStream);
80: } else {
81: throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
82: }
83:
84: // compose result string (ip:port)
85: result = prop.getProperty("db_passwd");
86: } catch (Exception e) {
87: System.out.println("Exception: " + e);
88: } finally {
89: if (inputStream != null) {
90: inputStream.close();
91: }
92: }
93: return result;
94: }
SSP.Roles_rights.get_roles_rights(Roles_rights.java:52)
40: public void get_roles_rights(){
41:
42: // Get roles & rigths from db
43: // variables
44: JDBCConnection database_connection = null;
45: ResultSet m_ResultSet = null;
46:
47: // load standard db-user from config file
48: Load_config properties = new Load_config();
49:
50: // connect to postgreSQL Database
51: try {
52: database_connection = new JDBCConnection("Self-Service-Portal", properties.getPropValues_db_user(), properties.getPropValues_db_user_pw());
53: } catch (IOException ex) {
54: Logger.getLogger(User_management.class.getName()).log(Level.SEVERE, null, ex);
55: }
.......
114: }
SSP.Roles_rights.getInstance(Roles_rights.java:35)
24: public class Roles_rights {
25:
26: public List<List<Integer>> oe_fac_roles_rights = new ArrayList<List<Integer>>(4);
27:
28: private static Roles_rights instance = null;
29: private Roles_rights() {
30: // Exists only to defeat instantiation.
31: }
32: public static Roles_rights getInstance() {
33: if(instance == null) {
34: instance = new Roles_rights();
35: instance.get_roles_rights();
36: }
37: return instance;
38: }
39:
40: public void get_roles_rights(){
......
114: }
115: }
SSP.Startup.contextInitialized(Startup.java:21)
16: public class Startup implements javax.servlet.ServletContextListener {
17:
18: @Override
19: public void contextInitialized(ServletContextEvent sce) {
20: // create singleton object to get all roles/rights from db
21: Roles_rights tmp = Roles_rights.getInstance();
22: }
23:
24: @Override
25: public void contextDestroyed(ServletContextEvent sce) {
26: throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
27: }
28: }
以防我提供更多信息:
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="Self-Service-Portal" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee">
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>SSP.Login</servlet-class>
</servlet>
<servlet>
<servlet-name>Usermode</servlet-name>
<servlet-class>SSP.Usermode</servlet-class>
</servlet>
<servlet>
<servlet-name>Echo_usermode</servlet-name>
<servlet-class>SSP.Echo_usermode</servlet-class>
</servlet>
<servlet>
<servlet-name>User_management</servlet-name>
<servlet-class>SSP.User_management</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/portal.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Usermode</servlet-name>
<url-pattern>/usermode/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Echo_usermode</servlet-name>
<url-pattern>/echo_usermode</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>User_management</servlet-name>
<url-pattern>/user_management/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>SSP.Startup</listener-class>
</listener>
我的项目结构
此外,Netbeans向我显示了web.xml的错误:
cvc-elt.1: Cannot find the declaration of element 'web-app'
因为这确实显示过,并且从未引起过问题我不知道这可能是原因
是否有任何人知道为什么我的应用程序不会因为我的类现在在SSP包内而不是默认包而因为我在web.xml中调整了包更改?
问候 Alkahna
答案 0 :(得分:1)
您尝试关闭输入流即使它为空。考虑转而使用try-with-resource。或者至少在关闭之前检查inputStream为!= null
。
73: try {
76: inputStream = getClass().getResourceAsStream(propFileName);
77
78: if (inputStream != null) {
80: } else {
82: }
83:
88: } finally {
89: inputStream.close(); //Might be null -> cause of your error
90: }
至于为什么“null”的原因:检查属性文件是否已部署到您认为应该存在的位置。尝试使用getClass().getResourceAsStream()
而不是使用classloader方法。尝试使用绝对路径/SSP/db_settings.properties
。