4

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.

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, getProperty is returning null as it is not found. Any ideas of what it could be or what I'm doing wrong?

4
  • 3
    What's the contents of your properties file? Commented Oct 2, 2013 at 12:31
  • Where is your property file located? Is it in class path?
    – Vimal Bera
    Commented Oct 2, 2013 at 12:32
  • No exception being thrown in the constructor?
    – DaveH
    Commented Oct 2, 2013 at 12:36
  • So - you're getting NULL returned from the prop.getProoperty("url1"), rather than a NullPointerException? To me, this implies the props file file is either empty, or its contents are not what you except, or the capitalisation of the entries in the props file does not match that in your code.
    – DaveH
    Commented Oct 2, 2013 at 13:28

2 Answers 2

6

Another wild guess: I noticed that both your prop variable 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:

static final Properties prop = new Properties();
static {
    try{
        prop.load(new FileInputStream("db.properties"));
    }catch(Exception e){
        logger.info("file not found.");
        e.printStackTrace();
    }
}
2
  • It worked, i deleted my constructor and add the code above. But is it "correct" to do that ? looks like a hotfix.
    – elcharrua
    Commented Oct 2, 2013 at 13:02
  • Well, you could also make it a Singleton object, but I think this is perfectly okay.
    – tobias_k
    Commented Oct 2, 2013 at 13:09
3

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.

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();
        }
    }
}
5
  • Oh, Excuse me!, you're right. The problem is in the content of file Commented Oct 2, 2013 at 12:46
  • @BlackSlash, I have edited my comment in order to not create confusion. Thanks! Commented Oct 2, 2013 at 12:53
  • So, you edited this to basically the same thing that I answered before?
    – tobias_k
    Commented Oct 2, 2013 at 13:17
  • Yes, I saw it after apply my edition :-(. But I was between edit it again or delete it, and I decided not delete. I hope this don't disturb you ;-) Regards, Commented Oct 2, 2013 at 15:33
  • No, it's okay. I was a bit grumpy first, because it looked as if your answer was there first (older answer timestamp) but that's okay. Never mind. :-)
    – tobias_k
    Commented Oct 2, 2013 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.