java.lang.NullPointerException: 在加载属性文件时,inStream 参数为空。

huangapple go评论84阅读模式
英文:

java.lang.NullPointerException: inStream parameter is null while loading a properties file

问题

在这里有什么做得不对的地方?

英文:

I have created a ZK web application project in Eclipse, and deployed the same in Apache Tomcat 9.0.37. And I have a properties file to be read(aws.properties) - which is located right under WEB-INF.

I am having difficulties loading the properties file - getting a NullpointeException

Eclipse ZK Project

java.lang.NullPointerException: 在加载属性文件时,inStream 参数为空。

Project Structure:

java.lang.NullPointerException: 在加载属性文件时,inStream 参数为空。

I tried all the 3 approaches below and all of them throws a java.lang.NullPointerException: inStream parameter is null at the line PROPERTIES.load(inputStream);

Code:

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);

Stacktrace:

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)

Question:

What is being done incorrectly here?

答案1

得分: 2

你的属性文件应该放在WEB-INF/classes目录下。类加载器不会在WEB-INF目录下查找单独的文件,因为Servlet规范规定它不应该在那里查找单独的文件。

英文:

Your properties file belongs in WEB-INF/classes. The class loader will not look for individual files in WEB-INF, because the servlet specification says it should not look for individual files there.

答案2

得分: 0

除了VGR的回答之外,一个.properties文件可以直接从WEB-INF文件夹中读取。与通过类加载器从类路径加载不同,它可以从ServletContext对象中读取。

这假设您可以访问一个ServletContext对象。下面的示例显示了如何通过WebApps.getCurrent()实例获取ServletContext。

// 使用ZK特定的WebApps工具
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")

不过,我更倾向于将配置文件保留在类路径中,而不是使Dao类依赖于ServletContext的存在。然而,您的代码示例没有明确指出配置是在哪个层次读取的。

英文:

In addition to VGR's response a .properties file can be read from within the WEB-INF folder directly. Instead of loading it from the classpath via classloader it can be read from a ServletContext object.

That assumes you have access to a ServletContext object. The example below shows how to obtain the ServletContext from via WebApps.getCurrent() instance.

// 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);

A shortcut would be to call WebApps.getCurrent().getResourceAsStream("/WEB-INF/aws.properties") directly.

Still I'd prefer to keep the config file in the classpath and not to make the Dao-class dependent on the existence of a ServletContext. However your code example wasn't specific about at which layer your configuration is being read.

huangapple
  • 本文由 发表于 2020年7月21日 19:08:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63013190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定