0

I'm trying to externalize a properties file. Right now, I'm testing by placing my properties file under my user profile folder, but I'm getting a NullPointerException. To be more specific, the System.out.println statement isn't being displayed in the console, which leads me to think that maybe it's the environment variable that's triggering the NullPointerException. I have also ensured that I placed the properties file in the correct path. Here is part of the code:

@PropertySource("file:${USERPROFILE}/test.properties")
public MyClass{

 private static Environment e;

 public static void main(String[] args){
    System.out.println("Test properties file: " + e.getProperty("myKey");
 }

}

Properties file:

myKey=testvalue

And this line in my dispatcher servlet xml file :

<context:property-placeholder location="classpath*:query.properties, file:${USERPROFILE}/test.properties"/>

Where am I going wrong here? Any help would be greatly appreciated.

user2554121
  • 225
  • 1
  • 7
  • 17
  • 2
    Look like `e` is null. Where do you initialize its value? – MFisherKDX Oct 25 '17 at 21:13
  • @MFisherKDX, I tried initializing it to null where I declared it. But I get the NullPointerException with or without initializing it there, unless I'm missing something, is there somewhere else it needs to be initialized in Spring. I'm new to Spring – user2554121 Oct 25 '17 at 21:34
  • Correct. That would be the expected behavior. .https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it. I don't know anything about Spring. – MFisherKDX Oct 25 '17 at 21:38
  • Have you tried injecting the application context and then saying: Envrionment e = context.getEnvrionment()? This should avoid null on e variable – aarnaut Oct 25 '17 at 22:15

1 Answers1

0

Looks like your Environment object is not loaded and binded and is null. Try to inject the Environment variable using @Resource

Replace

private static Environment e;

With

@Resource
private static Environment e;
mhasan
  • 3,703
  • 1
  • 18
  • 37