线程 "main" java.lang.ExceptionInInitializerError 中的异常由以下原因引起:java.lang.NullPointerException
Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException
我正在使用 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)
主要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;
}
}
我无法理解错误是什么...
看起来你的代码在 maven 项目中。因此,
- 将您的属性文件放在
src/main/resources/config.properties
- 使用
getResourceAsStream("/config.properties")
在进行 maven 构建时,maven 将打包您的 jar 并将资源作为类路径的一部分。 resources/
中的任何内容都将成为类路径根目录的一部分,因为我在使用 getResourceAsStream
.
时以斜杠开头
您也可以简单地调用:
HttpServer.class.getResourceAsStream("/config.properties")
相反。
请注意,您打开一个 InputStream,并将其传递给 Properties.load()
。这将使流保持打开状态。最好做类似的事情:
try (InputStream is = HttpServer.class.getResourceAsStream("/config.properties") ) {
properties.load(is);
}
Try-With-Resources 无论如何都会关闭输入流(即使是 error/exception)。
很多人不这样做,甚至我也让它短暂打开 运行 程序......但你的建议它是一个 HTTP 服务器......所以最好对这些遮罩更加敏感.. . 连接泄漏、文件句柄泄漏、内存泄漏等。最终它可能会被垃圾收集,但最好不要依赖它。
我正在使用 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)
主要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;
}
}
我无法理解错误是什么...
看起来你的代码在 maven 项目中。因此,
- 将您的属性文件放在
src/main/resources/config.properties
- 使用
getResourceAsStream("/config.properties")
在进行 maven 构建时,maven 将打包您的 jar 并将资源作为类路径的一部分。 resources/
中的任何内容都将成为类路径根目录的一部分,因为我在使用 getResourceAsStream
.
您也可以简单地调用:
HttpServer.class.getResourceAsStream("/config.properties")
相反。
请注意,您打开一个 InputStream,并将其传递给 Properties.load()
。这将使流保持打开状态。最好做类似的事情:
try (InputStream is = HttpServer.class.getResourceAsStream("/config.properties") ) {
properties.load(is);
}
Try-With-Resources 无论如何都会关闭输入流(即使是 error/exception)。
很多人不这样做,甚至我也让它短暂打开 运行 程序......但你的建议它是一个 HTTP 服务器......所以最好对这些遮罩更加敏感.. . 连接泄漏、文件句柄泄漏、内存泄漏等。最终它可能会被垃圾收集,但最好不要依赖它。