Java 属性 getProperty 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19136911/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:26:12  来源:igfitidea点击:

Properties getProperty returning null

java

提问by elcharrua

I'm having the following problem. I'm using Java properties to read some info of a file, but when I call prop.getProperty("var")it returns null. I ran out of ideas. Here is the code I have.

我有以下问题。我正在使用 Java 属性来读取文件的一些信息,但是当我调用prop.getProperty("var")它时返回null. 我的想法用完了。这是我的代码。

static final Properties prop = new Properties();
public JConnection(){
    try{
        prop.load(new FileInputStream("db.properties"));
    }catch(Exception e){
        logger.info("file not found.");
        e.printStackTrace();
    }
}

I never get the error message "file not found".

我从来没有收到错误消息“找不到文件”。

 public static Connection getConnection(String conType) {
    Connection conn;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver"); 
        if(model == "client"){
             conn = DriverManager.getConnection(prop.getProperty("url"),prop.getProperty("usr"),prop.getProperty("pass"));  
        }else{
            conn = DriverManager.getConnection(prop.getProperty("url1"),prop.getProperty("usr1"),prop.getProperty("pass1")); 
        }
    } catch (Exception ex) {            
        ex.printStackTrace();
        conn = null;
    }

When it tries to connect to the DB, getPropertyis returning nullas it is not found. Any ideas of what it could be or what I'm doing wrong?

当它尝试连接到数据库时,由于未找到getProperty而返回null。关于它可能是什么或我做错了什么的任何想法?

采纳答案by tobias_k

Another wild guess: I noticed that both your propvariable and the method that's reading from it are static, so maybe you are using this as some sort of static utilities class without ever creating an instance of the class? In this case, you are never calling the constructor and never actually loading the properties file. Instead, you might try this:

另一个疯狂的猜测:我注意到您的prop变量和从中读取的方法都是static,所以也许您将它用作某种静态实用程序类而从未创建该类的实例?在这种情况下,您永远不会调用构造函数,也不会实际加载属性文件。相反,你可以试试这个:

static final Properties prop = new Properties();
static {
    try{
        prop.load(new FileInputStream("db.properties"));
    }catch(Exception e){
        logger.info("file not found.");
        e.printStackTrace();
    }
}

回答by angel_navarro

You have a static field (prop), but you initialize it in a constructor. This means if you consult your prop object before you construct any object of JConnection, prop will not be initialized.

您有一个静态字段 (prop),但您在构造函数中对其进行了初始化。这意味着如果您在构造 JConnection 的任何对象之前咨询您的 prop 对象,prop 将不会被初始化。

You can try something like this:

你可以尝试这样的事情:

public class JConecction {
    static final Properties prop = new Properties();

    static {
        try {
            prop.load(new FileInputStream("db.properties"));
        } catch(Exception e) {
            logger.info("file not found.");
            e.printStackTrace();
        }
    }
}