NullPointerException when reading a properties file in Java

前端 未结 11 1375
我在风中等你
我在风中等你 2020-12-09 17:05

I am using the following code to read a properties file:

Properties pro = new Properties();
InputStream is = Thread.currentThread().getContextClassLoader().         


        
11条回答
  •  暖寄归人
    2020-12-09 17:14

    I had the same problem and this helped me:

    InputStream is;
    try {
    
        is = this.getClass().getClassLoader().getResourceAsStream("config.properties");
    
        prop.load(is);
    
        String url = prop.getProperty("url");
        String user = prop.getProperty("user");
        String pass = prop.getProperty("password");
        is.close();
        // opening database connection to MySQL server
        con = DriverManager.getConnection(url, user, pass);
    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    

提交回复
热议问题