TransWikia.com

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

Stack Overflow Asked by Sandeep Chatterjee on November 4, 2021

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

Eclipse ZK Project

Project Structure:

Project Structure

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?

2 Answers

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.

Answered by cor3000 on November 4, 2021

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.

Answered by VGR on November 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP