加载属性文件时出现空指针异常
Getting Null pointer exception while loading properties file
我正在尝试在 Maven 项目 (IntelliJ IDE) 中加载属性文件。
加载时我遇到空指针异常。
异常
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.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)
下面是一段代码
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();
项目的文件夹结构
下面是classloader的结果:
ParallelWebappClassLoader
context: Dashboard
delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@675d3402
我几乎在每一页上都搜索了修复程序,但仍然不走运。这就是为什么我不得不 post 它作为新问题。
如果有任何语法错误,请原谅。
并感谢您的帮助。
将 db.properties
文件放入 src/main/resources
文件夹,然后您应该可以访问它
首先创建以下结构:
src/main/java/%你所有的包裹和 类 这里%
src/main/resources/%将所有非 java 代码放在这里%
然后,右键单击资源文件夹并将其标记为 Resources Root,对 java 文件夹执行相同操作,但将其标记为 Sources Root。
构建项目,您应该能够使用
加载db.properties
System.class.getResourceAsStream("/path/to/db.properties");
希望这对您有所帮助。如果您遇到更多问题,请随时在下方发表评论。
这对我有用:
test.properties 保存在 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"));
}
}
控制台输出:
JohnDoe
Process finished with exit code 0
IDE 遵循 Maven 规则。由于默认情况下 Maven 不会复制不在 src/main/resources
文件夹中的资源。您应该将它们复制到此文件夹或配置 Maven 以在编译时包含它们,例如:
<build>
...
<resources>
<resource>
<directory>${project.build.sourceDirectory}</directory>
<includes>
<include>**/*.vm</include>
</includes>
</resource>
</resources>
...
</build>
我正在尝试在 Maven 项目 (IntelliJ IDE) 中加载属性文件。 加载时我遇到空指针异常。
异常
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.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)
下面是一段代码
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();
项目的文件夹结构
下面是classloader的结果:
ParallelWebappClassLoader
context: Dashboard
delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@675d3402
我几乎在每一页上都搜索了修复程序,但仍然不走运。这就是为什么我不得不 post 它作为新问题。 如果有任何语法错误,请原谅。 并感谢您的帮助。
将 db.properties
文件放入 src/main/resources
文件夹,然后您应该可以访问它
首先创建以下结构:
src/main/java/%你所有的包裹和 类 这里% src/main/resources/%将所有非 java 代码放在这里%
然后,右键单击资源文件夹并将其标记为 Resources Root,对 java 文件夹执行相同操作,但将其标记为 Sources Root。
构建项目,您应该能够使用
加载db.propertiesSystem.class.getResourceAsStream("/path/to/db.properties");
希望这对您有所帮助。如果您遇到更多问题,请随时在下方发表评论。
这对我有用:
test.properties 保存在 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"));
}
}
控制台输出:
JohnDoe
Process finished with exit code 0
IDE 遵循 Maven 规则。由于默认情况下 Maven 不会复制不在 src/main/resources
文件夹中的资源。您应该将它们复制到此文件夹或配置 Maven 以在编译时包含它们,例如:
<build>
...
<resources>
<resource>
<directory>${project.build.sourceDirectory}</directory>
<includes>
<include>**/*.vm</include>
</includes>
</resource>
</resources>
...
</build>