java.lang.NullPointerException: 加载属性文件时 inStream 参数为空
java.lang.NullPointerException: inStream parameter is null while loading a properties file
我在 Eclipse
中创建了一个 ZK
web application project
,并在 Apache Tomcat 9.0.37
中部署了它。我有一个要读取的属性文件 (aws.properties
) - 它位于 WEB-INF
.
正下方
我在加载属性文件时遇到困难 - 获取 NullpointeException
Eclipse ZK 项目
项目结构:
我尝试了下面的所有 3 种方法,它们都在 PROPERTIES.load(inputStream);
行抛出 java.lang.NullPointerException: inStream parameter is null
代码:
private static final String PROPERTIES_FILE = "aws.properties";
private static final Properties PROPERTIES = new Properties();
...
...
...
// APPROACH 1.
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
// APPROACH 2.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(PROPERTIES_FILE);
// APPROACH 3.
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(PROPERTIES_FILE);
PROPERTIES.load(inputStream);
堆栈跟踪:
java.lang.NullPointerException: inStream parameter is null
at java.base/java.util.Objects.requireNonNull(Objects.java:246)
at java.base/java.util.Properties.load(Properties.java:404)
at in.net.cadentic.core.database.AmazonDynamoDBClientHandler.createNewClient(AmazonDynamoDBClientHandler.java:75)
at in.net.cadentic.core.dao.UsermasterDao.findBypassword(UsermasterDao.java:27)
at in.net.cadentic.composers.LoginComposer.onClick$loginButton(LoginComposer.java:71)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.zkoss.zk.ui.event.GenericEventListener.onEvent(GenericEventListener.java:80)
at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:3184)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3154)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3096)
at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:138)
at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:353)
at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:377)
at org.zkoss.zk.ui.AbstractComponent$ForwardListener.onEvent(AbstractComponent.java:3629)
at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:3184)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3154)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3096)
at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:138)
at org.zkoss.zk.ui.impl.UiEngineImpl.processEvent(UiEngineImpl.java:1851)
at org.zkoss.zk.ui.impl.UiEngineImpl.process(UiEngineImpl.java:1623)
at org.zkoss.zk.ui.impl.UiEngineImpl.execUpdate(UiEngineImpl.java:1326)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.process(DHtmlUpdateServlet.java:611)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:487)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.doPost(DHtmlUpdateServlet.java:495)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)
问题:
哪里做错了?
您的属性文件属于 WEB-INF/classes。 class 加载器将不会在 WEB-INF 中寻找单独的文件,因为 servlet 规范规定它不应该在那里寻找单独的文件。
除了 之外,还可以直接从 WEB-INF 文件夹中读取 .properties
文件。与其通过 classloader 从 classpath 加载它,不如从 ServletContext
对象中读取它。
假设您有权访问 ServletContext 对象。下面的示例显示了如何从 WebApps.getCurrent()
实例获取 ServletContext。
// using ZK specific WebApps util
ServletContext servletContext = WebApps.getCurrent().getServletContext();
// Servlet API
InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/aws.properties");
Properties myProps = new Properties();
myProps.load(inputStream);
快捷方式是直接调用 WebApps.getCurrent().getResourceAsStream("/WEB-INF/aws.properties")
。
我仍然希望将配置文件保留在 class 路径中,而不是使 Dao-class 依赖于 ServletContext 的存在。但是,您的代码示例并未具体说明在哪一层读取您的配置。
我在 Eclipse
中创建了一个 ZK
web application project
,并在 Apache Tomcat 9.0.37
中部署了它。我有一个要读取的属性文件 (aws.properties
) - 它位于 WEB-INF
.
我在加载属性文件时遇到困难 - 获取 NullpointeException
Eclipse ZK 项目
项目结构:
我尝试了下面的所有 3 种方法,它们都在 PROPERTIES.load(inputStream);
java.lang.NullPointerException: inStream parameter is null
代码:
private static final String PROPERTIES_FILE = "aws.properties";
private static final Properties PROPERTIES = new Properties();
...
...
...
// APPROACH 1.
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
// APPROACH 2.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(PROPERTIES_FILE);
// APPROACH 3.
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(PROPERTIES_FILE);
PROPERTIES.load(inputStream);
堆栈跟踪:
java.lang.NullPointerException: inStream parameter is null
at java.base/java.util.Objects.requireNonNull(Objects.java:246)
at java.base/java.util.Properties.load(Properties.java:404)
at in.net.cadentic.core.database.AmazonDynamoDBClientHandler.createNewClient(AmazonDynamoDBClientHandler.java:75)
at in.net.cadentic.core.dao.UsermasterDao.findBypassword(UsermasterDao.java:27)
at in.net.cadentic.composers.LoginComposer.onClick$loginButton(LoginComposer.java:71)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.zkoss.zk.ui.event.GenericEventListener.onEvent(GenericEventListener.java:80)
at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:3184)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3154)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3096)
at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:138)
at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:353)
at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:377)
at org.zkoss.zk.ui.AbstractComponent$ForwardListener.onEvent(AbstractComponent.java:3629)
at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:3184)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3154)
at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:3096)
at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:138)
at org.zkoss.zk.ui.impl.UiEngineImpl.processEvent(UiEngineImpl.java:1851)
at org.zkoss.zk.ui.impl.UiEngineImpl.process(UiEngineImpl.java:1623)
at org.zkoss.zk.ui.impl.UiEngineImpl.execUpdate(UiEngineImpl.java:1326)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.process(DHtmlUpdateServlet.java:611)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:487)
at org.zkoss.zk.au.http.DHtmlUpdateServlet.doPost(DHtmlUpdateServlet.java:495)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)
问题:
哪里做错了?
您的属性文件属于 WEB-INF/classes。 class 加载器将不会在 WEB-INF 中寻找单独的文件,因为 servlet 规范规定它不应该在那里寻找单独的文件。
除了 .properties
文件。与其通过 classloader 从 classpath 加载它,不如从 ServletContext
对象中读取它。
假设您有权访问 ServletContext 对象。下面的示例显示了如何从 WebApps.getCurrent()
实例获取 ServletContext。
// using ZK specific WebApps util
ServletContext servletContext = WebApps.getCurrent().getServletContext();
// Servlet API
InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/aws.properties");
Properties myProps = new Properties();
myProps.load(inputStream);
快捷方式是直接调用 WebApps.getCurrent().getResourceAsStream("/WEB-INF/aws.properties")
。
我仍然希望将配置文件保留在 class 路径中,而不是使 Dao-class 依赖于 ServletContext 的存在。但是,您的代码示例并未具体说明在哪一层读取您的配置。