[英]Exception in thread “main” java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException
I am using config.properties file in order to set port. 我正在使用config.properties文件来设置端口。 After running I am facing an error:
运行后,我遇到一个错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
线程“主”中的异常java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException at
由以下原因引起:java.lang.NullPointerException
java.util.Properties$LineReader.readLine(Properties.java:434) at
java.util.Properties $ LineReader.readLine(Properties.java:434)在
java.util.Properties.load0(Properties.java:353) at
java.util.Properties.load0(Properties.java:353)在
java.util.Properties.load(Properties.java:341) at
java.util.Properties.load(Properties.java:341)在
HttpServer.setPort(HttpServer.java:83) at
HttpServer.setPort(HttpServer.java:83)在
HttpServer.(HttpServer.java:26)
的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... 我不明白这是什么错误...
It looks like your code is in a maven project. 看起来您的代码在maven项目中。 As such,
因此,
src/main/resources/config.properties
src/main/resources/config.properties
getResourceAsStream("/config.properties")
getResourceAsStream("/config.properties")
When doing a maven build, maven will package your jar and make the resources part of the classpath. 在进行Maven构建时,Maven会将您的jar打包,并使资源成为类路径的一部分。 Anything in
resources/
will be part of the classpath root, since I start it with a slash when I use the getResourceAsStream
. 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()
. 请注意,您打开了InputStream,并将其传递给
Properties.load()
。 This will leave the stream open. 这将使流保持打开状态。 Better to do something like:
最好做这样的事情:
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服务器...因此最好对这些遮罩更加敏感...连接泄漏,文件句柄泄漏,内存泄漏等。最终可能会收集垃圾,但最好不要依赖它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.