简体   繁体   English

线程“主”中的异常java.lang.ExceptionInInitializerError原因:java.lang.NullPointerException

[英]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, 因此,

  1. place your properties file in src/main/resources/config.properties 将您的属性文件放在src/main/resources/config.properties
  2. use 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.

相关问题 java.lang.ExceptionInInitializerError原因:java.lang.NullPointerException - java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException 由 java.lang.RuntimeException 引起的线程“main”java.lang.ExceptionInInitializerError 中的异常:无法实例 KieServices - Exception in thread "main" java.lang.ExceptionInInitializerError caused by java.lang.RuntimeException: Unable to instance KieServices 在线程“主”中获取异常java.lang.ExceptionInInitializerError异常 - Getting Exception in thread “main” java.lang.ExceptionInInitializerError Exception 配置中的无效映射导致线程“主”中的休眠映射异常java.lang.ExceptionInInitializerError - Hibernate Mapping Exception in thread “main” java.lang.ExceptionInInitializerError caused by Invalid Mapping at configuration 线程“主”中的异常java.lang.ExceptionInInitializerError(Clojure) - Exception in thread “main” java.lang.ExceptionInInitializerError (Clojure) 线程“主”中的休眠异常java.lang.ExceptionInInitializerError - Hibernate Exception in thread “main” java.lang.ExceptionInInitializerError 获取错误“线程“ main”中的异常java.lang.ExceptionInInitializerError” - Getting Error “Exception in thread ”main“ java.lang.ExceptionInInitializerError” Spring Error-线程“ main”中的异常java.lang.ExceptionInInitializerError - Spring Error- Exception in thread “main” java.lang.ExceptionInInitializerError 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException “线程“ main”中的异常java.lang.NullPointerException” - “Exception in thread ”main“ java.lang.NullPointerException”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM