0

I have a 'PropertiesUtil' class for reading properties from a configuration file. The code of the class is given below:

public class PropertiesUtil {

  public static Properties prop;
  public static String databaseConnectivityString;
  public static String databaseUserName;
  public static String databaseUserPassword;

  static {
      try {
          prop.load(new FileReader(new File("config.prop")));
          databaseConnectivityString = prop.getProperty("databaseConnectivityString");
          databaseUserName = prop.getProperty("databaseUserName");
          databaseUserPassword = prop.getProperty("databaseUserPassword");
      } catch (IOException | NumberFormatException ex) {
          LoggerUtil.getLogger().severe("Problem loading or Reading Configuration file");
      }
  }
}

when I call the following code from another class 'Test' with a main method I get a NullPointerException. The code of the 'Test' Class is given below. Kindly help me troubleshoot.

public class Test {
  public static void main(String... args) throws IOException {
      System.out.println(PropertiesUtil.databaseUserName);
  }
}
1
  • check congig.pop is available in current class path or not ..then match the properties key with property name that you provided in your code Commented Mar 26, 2014 at 18:31

1 Answer 1

4

Initialize your field right away.

public static Properties prop = new Properties();

otherwise the field remains null and dereferencing it

prop.load(new FileReader(new File("config.prop")));

causes a NullPointerException.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.