Reputation: 125
I am trying to load the properties file in maven project(IntelliJ IDE). While loading i am facing null pointer exception.
Exception
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at main.com.bby.dao.EventListDao.getEventList(EventListDao.java:29)
at main.com.bby.controller.EventController.allEventLists(EventController.java:39)
at main.com.bby.controller.EventController.getAllEventLists(EventController.java:32)
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:498)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
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:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Below is the piece of code
Properties prop = new Properties();
Thread currentThread = Thread.currentThread();
ClassLoader contextClassLoader = currentThread.getContextClassLoader();
InputStream propFile = contextClassLoader.getResourceAsStream("db.properties");
prop.load(propFile);
System.out.println("Properties file loaded!!");
propFile.close();
Folder Structure of the project
Below is the result of classloader:
ParallelWebappClassLoader
context: Dashboard
delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@675d3402
I did search on almost every page for the fix but still not luck. That is why i had to post it as new question. Kindly excuse if any grammatical error. And thank you for the help.
Upvotes: 2
Views: 1577
Reputation: 16411
IDE follows Maven rules here. Since Maven by default will not copy resources which are not in src/main/resources
folder. You should either copy them to this folder or configure maven to include them on compile by, for example:
<build>
...
<resources>
<resource>
<directory>${project.build.sourceDirectory}</directory>
<includes>
<include>**/*.vm</include>
</includes>
</resource>
</resources>
...
</build>
Upvotes: 0
Reputation: 91
This works for me:
test.properties is saved in src/main/resources
test.properties:
com.test.name=JohnDoe
Application.java:
package com.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Application {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
try(InputStream is = Application.class.getResourceAsStream("/test.properties")) {
properties.load(is);
}
System.out.println(properties.getProperty("com.test.name"));
}
}
Console output:
JohnDoe
Process finished with exit code 0
Upvotes: 0
Reputation: 91
Create the following structure first:
src/main/java/%all your packages and classes here% src/main/resources/%put everything that is not java code here%
Then, right click on the resources folder and mark it as Resources Root, do the same for the java folder but instead mark it as Sources Root.
Build the project and you should be able to load db.properties using
System.class.getResourceAsStream("/path/to/db.properties");
Hope this helps. If you encounter further issues feel free to comment down below.
Upvotes: 0
Reputation: 12051
Put the db.properties
file in the src/main/resources
folder and then you should be able to access it
Upvotes: 3