线程“main”中的异常 java.lang.ExceptionInInitializerError 导致:java.lang.NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30550345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException
提问by jbakirov
I am using config.properties file in order to set port. After running I am facing an error:
我正在使用 config.properties 文件来设置端口。运行后,我面临一个错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: 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
HttpServer.setPort(HttpServer.java:83) at
HttpServer.(HttpServer.java:26)
线程“main”中的异常 java.lang.ExceptionInInitializerError
引起:java.lang.NullPointerException at
java.util.Properties$LineReader.readLine(Properties.java:434) 在
java.util.Properties.load0(Properties.java:353) 在
java.util.Properties.load(Properties.java:341) 在
HttpServer.setPort(HttpServer.java:83) 在
HttpServer.(HttpServer.java:26)
The main class:
主要类:
public class HttpServer {
static final boolean SSL = System.getProperty("ssl") != null;
static final int PORT = Integer.parseInt(System.getProperty("port", SSL ? "8443" : setPort()));
public static void main(String[] args) {
HttpServer httpServer = new HttpServer();
httpServer.start();
}
public void start(){}
public static String setPort() {
String port = "";
Properties properties = new Properties();
try {
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("src/main/config.properties"));
port = properties.getProperty("server.port");
} catch (IOException e) {
e.printStackTrace();
}
return port;
}
}
I am not able to understand what is the error...
我无法理解错误是什么...
回答by YoYo
It looks like your code is in a maven project. As such,
看起来您的代码在 Maven 项目中。像这样,
- place your properties file in
src/main/resources/config.properties
- use
getResourceAsStream("/config.properties")
- 将您的属性文件放在
src/main/resources/config.properties
- 利用
getResourceAsStream("/config.properties")
When doing a maven build, maven will package your jar and make the resources part of the classpath. Anything in resources/
will be part of the classpath root, since I start it with a slash when I use the getResourceAsStream
.
在进行 maven 构建时,maven 将打包您的 jar 并使资源成为类路径的一部分。中的任何内容都resources/
将成为类路径根的一部分,因为当我使用getResourceAsStream
.
You could also have simply called:
HttpServer.class.getResourceAsStream("/config.properties")
instead.
你也可以简单地调用:
HttpServer.class.getResourceAsStream("/config.properties")
代替。
Note that you open a InputStream, and pass it to Properties.load()
. This will leave the stream open. Better to do something like:
请注意,您打开了一个 InputStream,并将其传递给Properties.load()
. 这将使流保持打开状态。最好做这样的事情:
try (InputStream is = HttpServer.class.getResourceAsStream("/config.properties") ) {
properties.load(is);
}
The Try-With-Resources will take care of closing the input stream no matter what ( even in case of an error/exception).
无论如何, Try-With-Resources 将负责关闭输入流(即使出现错误/异常)。
Many do not do that, and even I leave it open for short running programs ... But yours suggests it is a HTTP Server ... so better to be more sensitive about these mattes ... connection leaks, file handle leaks, memory leaks, etc. Eventually it might get garbage collected, but better not to depend on it.
许多人不这样做,即使我为短期运行的程序打开它......但你的建议它是一个HTTP服务器......所以最好对这些遮罩更加敏感......连接泄漏,文件句柄泄漏,内存泄漏等。最终它可能会被垃圾收集,但最好不要依赖它。